"Back" button on Action bar - Android . How to go "back"?

21,755

Solution 1

I think the easiest way out is follows:

I am assuming that from activity A you are starting the activity B. Now from activity B you want to go back to activity A on pressing the top left back button on action bar. simply call this.finish() or ActivityName.this.finish() from there:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

This should finish your current activity. However if you have lot of activities, then you might have to do this in all the activities. To save yourself from that effort, you can make a class lets call it AbstractActivity; that extends Activity. Then you can extend all your other activity classes to extend that class(AbstractActivity). Inside AbstractActivity you can put the above code. So now that piece of code would be valid for all your activities and that feature would be implemented for all of them. Basically this sort of thing (Inheritance)can be used any time, when there are some common features which would be applicable to your many classes.

If you are receiving any errors, please do post your LogCat if you require further help. Hope this helps you.

Solution 2

just giving basic code given by @shobhit puri...

for invoking the action bar back button..add the following code in the onCreate() method along with the onOptionsItemSelected....

protected void onCreate(Bundle savedInstanceState) 
{
    ActionBar actionBar = getActionBar(); 
    actionBar.setDisplayHomeAsUpEnabled(true);
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.activity_information);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        this.finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
Share:
21,755

Related videos on Youtube

Shobhit Puri
Author by

Shobhit Puri

LinkedIn: https://www.linkedin.com/in/shobhitpuri/ Twitter: https://twitter.com/shobhit_puri Medium: https://medium.com/@shobhitpuri Blog: https://dev.to/shobhit Quora: https://www.quora.com/shobhit-puri Recent Blog Posts: How to change the text and theme of Google’s Sign-In button on Android? - published by AndroidPub Publication on Medium. How to write your own git commands like "git refresh"?

Updated on September 11, 2020

Comments

  • Shobhit Puri
    Shobhit Puri over 3 years

    enter image description here

    Action Bar

    I'm talking about the (Number 1, in the pic), button with a little arrow and the app icon and the top left side of the screen. It is automatically defined when we select the "Black activity" template.

    My app has a pretty huge hierarchy graph, got about 25 activities now. I'm basically just showing a few tutorials and one can navigate to it according to the categories.

    Now that "Back" (?) button thingy on action bar is on every screen I have, and I do want to keep it. The code shows no error, but when I actually press that button, the app stops working. What I want, is to just replicate the actual back button function with that (Number 1) button, that I showed in the image. When I press it, the top most screen should close, and the last one should open. Just close the screen.

    What I tried :

    @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
    
                NavUtils.navigateUpFromSameTask(this);
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    

    this is the the function that inflates that buggy button along with the action bar. I tried to replace the entire code, and call "Finish" function, but that failed miserably. I was unable to find a function specifically made for that top left most button...

    I want the top most screen on the stack(the one in the foreground) to close, when this button is touched. How to do this ?

  • Admin
    Admin almost 11 years
    Hi there ! I tried that, but it still gave the same problem. However, I did one thing that made it work :- I deleted that entire switch() function's block, and instead put this.finish(); along with the default return super.onOptionsItemSelected(item); Will this cause any problems ?I mean deleting that function ? It seemed to work just fine... Also, In which file should I define that AbstractActivity Class ? Or a new separate class file ? and in what folder should I save it ?
  • Shobhit Puri
    Shobhit Puri almost 11 years
    I am not sure what might be the repercussions of that. Does it finish the activity on clicking any button on the action bar(as we are not specifying the button)? For the second thing, if you want to go that way then the class can be a separate class in any same folder as other java files.
  • Shobhit Puri
    Shobhit Puri almost 11 years
    Also when you say that the code in the answer was not working, did you have getActionBar().setDisplayHomeAsUpEnabled(true); in your onCreate() function? It is to show the user that selecting home will return one level up rather than to the top level of the app. If you didn't then try with that in onCreate()
  • Admin
    Admin almost 11 years
    thanks for the help ! ....But it just hit me .."why the hell do I need another back button"...if they want to go back, simply pressing the hardware back button would be much easier and efficient...I decided to delete all the action bars in my app XD, no button no problem haha :)
  • Muhammad Babar
    Muhammad Babar over 9 years
    android.R.id.home added in api level 11 whats for the backward compatibility?
  • Shobhit Puri
    Shobhit Puri over 9 years
    For backward compatability, have you tried using the android-v7-appcompat library ? Is it not as available using that ?
  • Muhammad Babar
    Muhammad Babar over 9 years
    I'm already using appComapt. When using android.R.id.home i can see that the home id is added in api 11.
  • has19
    has19 almost 8 years
    why dont you simply call function onBackPressed() instead of finish so android handle that instead of us
  • Shobhit Puri
    Shobhit Puri almost 8 years
    @has19 Its almost the same. If you want to do some thing on both back and up button press, you can do that. Under the hood, super.onBackPressed() calls public static void finishAfterTransition(Activity activity) { if (Build.VERSION.SDK_INT >= 21) { ActivityCompat21.finishAfterTransition(activity); } else { activity.finish(); } }