Android - back button in title bar

In many applications (Calendar, Drive, Play Store), when you press the button and enter a new action, the icon in the title bar turns into the "Back" button, but for the application I create, it does not do that. How to make this icon return to the previous screen?

+92
android user-interface themes titlebar
Jan 27 '13 at 6:51
source share
22 answers

There are two simple steps to creating a back button in the title bar:

First, make the application icon clickable using the following code in the exercise, in the title bar of which you want to add the "Back" button:

ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); 

After adding the above code, a back arrow will appear to the left of the application icon.

enter image description here

Secondly, after you have done the above, you still need to create code that will take advantage of the click of a button. To do this, onOptionsItemSelected in onOptionsItemSelected that when you really click on the application icon, the onOptionsItemSelected method is onOptionsItemSelected . Therefore, to return to the previous action, add this method to your action and insert Intent code in it, which will return you to the previous action. For example, suppose that you are trying to return MyActivity to MyActivity , called MyActivity . To get back to this, write a method as follows:

 public boolean onOptionsItemSelected(MenuItem item){ Intent myIntent = new Intent(getApplicationContext(), MyActivity.class); startActivityForResult(myIntent, 0); return true; } 

It!

(The API for Android developers recommends messing around with the manifest and adding things like android:parentActivityName . But this doesn't seem to work for me. The above is simpler and more reliable.)

 <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" /> 

And in your work

 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
+126
May 26 '13 at
source share
β€” -

use this code

  @Override public void onCreate(Bundle savedInstanceState) { ... getActionBar().setDisplayHomeAsUpEnabled(true); } 

after that write this code in the onOptionsItemSelected method

  int id = item.getItemId(); if (id==android.R.id.home) { finish(); } 
+54
Jun 26 '14 at 4:12
source share

I finally managed to correctly add the back button to the action bar / toolbar

 @Override public void onCreate(Bundle savedInstanceState) { ... getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; } return super.onOptionsItemSelected(item); } public boolean onCreateOptionsMenu(Menu menu) { return true; } 
+36
Nov 30 '14 at 12:00
source share

1.- Add activity to AndroidManifest.xml and make sure you provide metadata:

 <activity android:name="com.example.myfirstapp.DisplayMessageActivity" android:label="@string/title_activity_display_message" android:parentActivityName="com.example.myfirstapp.MainActivity" > <!-- Parent activity meta-data to support 4.0 and lower --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.myfirstapp.MainActivity" /> </activity> 

2.- Add the following code to the onCreate activity method:

 @Override public void onCreate(Bundle savedInstanceState) { ... getActionBar().setDisplayHomeAsUpEnabled(true); } 

3.- Override onOptionsItemSelected and use the static method NavUtils.navigateUpFromSameTask () to move the stack throw.

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Respond to the action bar Up/Home button case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } 

However, using navigateUpFromSameTask () is only suitable when your application is the owner of the current task (i.e. the user started this task from your application). If this is not the case, and your activity has been launched into a task related to another application, then to navigate upwards, create a new task that belongs to your application, which requires you to create a new back stack.

+16
Nov 11 '13 at 0:28
source share

If your activity extends Activity

 public class YourActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_xxx); getActionBar().setHomeButtonEnabled(true); [...] } [...] } 

If your action extends AppCompatActivity

 public class YourActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_xxx); getSupportActionBar().setHomeButtonEnabled(true); [...] } [...] } 

Nothing more, see Add action

[OPTIONAL] To explicitly identify parental activity, modify your .xml manifest as follows:

 <application ... > ... <!-- The main/home activity (it has no parent activity) --> <activity android:name="com.example.myfirstapp.MainActivity" ...> ... </activity> <!-- A child of the main activity --> <activity android:name="com.example.myfirstapp.YourActivity " android:label="@string/title_activity_display_message" android:parentActivityName="com.example.myfirstapp.MainActivity" > <!-- Parent activity meta-data to support 4.0 and lower --> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.myfirstapp.MainActivity" /> </activity> </application> 

See Specify Parental Activity

+9
13 Sep '16 at 10:23
source share

The easiest way and best practices, as Google explains here :

1. Add a parent for your childActivity in AndroidManifest.xml :

 <activity android:name=".ChildActivity" android:parentActivityName=".ParentActivity" > </activity> 

2. Activate the back button in your child.

 myActionOrActionSupportBar.setDisplayHomeAsUpEnabled(true); 

Worked for me, I hope this works for you too.

+5
Aug 31 '16 at 15:49
source share

first of all, add the following line to the onCreate function

 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

and then add the following function in code:

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; } return super.onOptionsItemSelected(item); } 
+5
Apr 23 '18 at 13:29
source share

If you are using the new support library for 5.1 in Android studio, you can use it instead on your AppCompatActivity

  ActionBar actionBar = getSupportActionBar(); actionBar.setHomeButtonEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setHomeAsUpIndicator(R.mipmap.ic_arrow_back_white_24dp); actionBar.setDisplayShowHomeEnabled(true); 

greetings.

+4
Jul 25 '15 at 7:27
source share

First you need to write this code

 @Override protected void onCreate(Bundle savedInstanceState) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); } 

Then add this line to the manifest

  <activity android:name=".MainActivity" android:parentActivityName=".PreviousActivity"></activity> 

I think it will work

+4
Nov 24 '18 at 12:55
source share

If your activity continues with AppCompatActivity , you need to override the onSupportNavigateUp() method as follows:

 public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); ... } @Override public void onBackPressed() { super.onBackPressed(); this.finish(); } @Override public boolean onSupportNavigateUp() { onBackPressed(); return true; } } 

Process your logic in the onBackPressed() method and simply call this method in onSupportNavigateUp() so that the back button on the phone and the arrow on the toolbar do the same.

+3
Mar 25 '17 at 8:21
source share

A lightweight version without using an ActionBarActivity that still has the same baaviors here:

 public class ToolbarConfigurer implements View.OnClickListener { private Activity activity; public ToolbarConfigurer(Activity activity, Toolbar toolbar, boolean displayHomeAsUpEnabled) { toolbar.setTitle((this.activity = activity).getTitle()); if (!displayHomeAsUpEnabled) return; toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha); toolbar.setNavigationOnClickListener(this); } @Override public void onClick(View v) { NavUtils.navigateUpFromSameTask(activity); } } 

Usage: Put new ToolbarConfigurer(this, (Toolbar) findViewById(R.id.my_awesome_toolbar), true); in onCreate .

+2
Dec 13 '14 at 15:27
source share

If you use an ActionBar, you will want to read this documentation http://developer.android.com/reference/android/app/ActionBar.html#setDisplayHomeAsUpEnabled(boolean)

Then you need to overwrite the onOptionsItemSelected (MenuItem) method and see which Android.R.id.home event will fire. Then you know that the user clicked on this button in the action bar

+1
Jan 27 '13 at 6:53
source share

You need to add the code below to the manifest file. Find the activity in which you want to add back arrow functionality. If you find one that then stop or create an action

 <activity android:name=".SearchActivity"> </activity> 

Then add the following three lines of code between them.

 <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.raqib.instadate.MainActivity" /> 

And don't forget to add this piece of code to onCreate (); a method of your specific activity in which you need a back arrow.

  Toolbar toolbar = (Toolbar) findViewById(R.id.searchToolbar); setSupportActionBar(toolbar); try{ getSupportActionBar().setDisplayHomeAsUpEnabled(true); }catch(NullPointerException e){ Log.e("SearchActivity Toolbar", "You have got a NULL POINTER EXCEPTION"); } 

This is how I solved the problem. Thank.

+1
Dec 18 '16 at 17:14
source share

I needed to mix several answers in order to get the right answer for me, because in my application there are 3 actions that can be returned at any time. Activity 1> Activity 2> Activity 3. When I did something in my activity 3, the "Back" button correctly completed the task "Activity 2". However, from Activity2, using finish() , it returned to Activity3, not Activity1. And I am expanding AppCompatActivity. So my solution was:

 public class Activity2 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { ... getSupportActionBar().setHomeButtonEnabled(true); } } 

on AndroidManifest.xml:

 <activity android:name=".activities.Activity2" android:parentActivityName="com.example.appname.activities.Activity1"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.appname.activities.Activity1" /> </activity> 

and finally, the action button in my menu (action bar):

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

Using NavUtils.navigateUpFromSameTask(this); worked for me, not finish() .

+1
Apr 07 '19 at 21:51
source share

You can also just put onBackPressed() in your onClick listener. This makes your button act like the default back / up buttons in Android apps!

0
Jul 09 '15 at 14:55
source share

You can also do this without code by specifying the parent activity in the manifest of the application. If you want the back button in Activity B to go to Activity A, just add Activity A as the parent of Activity B in the manifest.

0
Jun 30 '17 at 16:57
source share
 Toolbar toolbar=findViewById(R.id.toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); if (getSupportActionBar()==null){ getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); } @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId()==android.R.id.home) finish(); return super.onOptionsItemSelected(item); } 
0
Jul 07 '18 at 6:56
source share

This works for me .. Suppose there are two types of activity (Activityone, Activitytwo)

Inside Activity, we all use this code

 @Override protected void onCreate(Bundle savedInstanceState) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); } 

On Activityone

 //when you need to go second activity startActivity(new Intent(Activityone.this, Activitytwo.class)); 

This should be included in the second action inside the manifest file.

 <activity android:name=".Activitytwo" android:parentActivityName=".Activityone"></activity> 

And the result will be like this

enter image description here

0
Oct. 22 '18 at 12:07
source share

For kotlin:

  override fun onOptionsItemSelected(item: MenuItem): Boolean { onBackPressed(); return true; } 
0
Jan 30 '19 at 8:22
source share

Other answers do not mention that you can also set this in the XML of your Toolbar widget:

app:navigationIcon="?attr/homeAsUpIndicator"

For example:

 <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:navigationIcon="?attr/homeAsUpIndicator" app:popupTheme="@style/AppTheme.PopupOverlay" app:title="@string/title_activity_acoustic_progress" /> 
0
May 13 '19 at 2:33
source share

Just sharing what has helped me and may be useful to others. Although most of the answers here are correct, using getActionBar().setDisplayHomeAsUpEnabled(true); this does not work for me. The problem I ran into was that I tried to create the second action manually, but it has more details.
What really solved my problem was following the tutorial for Android developers ( https://developer.android.com/training/basics/firstapp/starting-activity ) to create a second lesson using Android Studio’s own tools:

 Create the second activity 1. In the Project window, right-click the app folder and select New > Activity > Empty Activity. 2. In the Configure Activity window, enter "DisplayMessageActivity" for Activity Name and click Finish (leave all other properties set to the defaults). Android Studio automatically does three things: - Creates the DisplayMessageActivity file. - Creates the corresponding activity_display_message.xml layout file. - Adds the required <activity> element in AndroidManifest.xml. 
0
Jun 24 '19 at 5:48
source share

This works for me getSupportActionBar (). SetDisplayHomeAsUpEnabled (false); enter image description here

-one
Oct 31 '18 at 17:22
source share



All Articles