“Back” button on Action bar - Android . How to go

2020-04-03 17:43发布

问题:

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 ?

回答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.



回答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);
}