In many apps (Calendar, Drive, Play Store) when you tap a button and enter a new activity, the icon in the title bar turns into a back button, but for the app I am making, i
The other answers don't 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" />
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);
}
This is working for me.. Suppose there are two activity (Activityone,Activitytwo)
Inside Activitytwo 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 second activity inside manifest file
<activity android:name=".Activitytwo"
android:parentActivityName=".Activityone"></activity>
And The result would be like this
The simplest way and best practice as google explains in here :
1.Add a parent for your childActivity in the AndroidManifest.xml
:
<activity
android:name=".ChildActivity"
android:parentActivityName=".ParentActivity" >
</activity>
2.Activate the back button in your childActivity :
myActionOrActionSupportBar.setDisplayHomeAsUpEnabled(true);
Worked for me, I hope it works for you too.
Just sharing something that helped me and may be useful for others. Although most of the answers here are correct, by using the getActionBar().setDisplayHomeAsUpEnabled(true);
, this wasn't working for me. The issue I had was that I was trying to create a second Activity manually, but there are more details involved.
What really solved my problem was following Android Developers tutorial (https://developer.android.com/training/basics/firstapp/starting-activity) for creating a second Activity using Android Studio 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.
All you need to do in 2020:
(considering you want to return to the MainActivity)
protected void onCreate(Bundle savedInstanceState){
...
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public boolean onOptionsItemSelected(MenuItem item) {
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
return true;
}