The back button on the action bar is Android. How to go back?

Picture

! Action bar

I’m talking about (number 1, in the figure), a button with a small arrow and an application icon and the upper left side of the screen. It is automatically detected when you select the Black Activity template.

My application has a rather huge hierarchy chart, now it has about 25 activities. I just show a few guides, and you can go to them according to categories.

Now that the back button (?) On the action bar is on every screen, and I want to save it. The code does not show an error, but when I click this button, the application stops working. What I want is to simply repeat the actual function of the "Back" button using the button (Number 1) that I showed in the image. When I click it, the top screen should close and the last 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); } 

it is a function that inflates this button with an error along with the action bar. I tried replacing all the code and calling the Finish function, but that was unsuccessful. I could not find a function specially created for this upper left button ...

I want the top screen in the stack (the one in the foreground) to close when this button has been touched. How to do it?

+7
java android eclipse button action
source share
2 answers

I think the easiest way out:

I assume that from activity A you start action B Now from activity area B you want to return to action A when you click the top left button on the action bar. just 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 complete your current activity. However, if you have many activities, you may need to do this in all activities. To save yourself from this effort, you can make a class by calling it AbstractActivity ; which continues to Activity . Then you can extend all other activity classes to extend this class ( AbstractActivity ). Inside AbstractActivity you can put the above code. So, now this piece of code will be valid for all your actions, and this function will be implemented for all of them. In principle, this kind of thing ( Inheritance ) can be used at any time when there are some common functions that apply to your many classes.

If you get any errors, send LogCat message if you need further help. Hope this helps you.

+30
source share

just specifying the base code of @shobhit puri ...

to invoke the action bar button .. and enter the following code in the onCreate () method along with 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); } 
+4
source share

All Articles