I have an application using the android DrawerLayout and the NavigationView to provide a drawer.
The drawer works great, and the menu items are active. When I click on a menu item that launches a new activity, the activity renders, and the toolbar shows a backarrow.
However, when I click the back arrow from the new activity, I do not return to the original activity with the DrawerLayout. Instead, I am backed out of the application altogether!
I had thought that if I specified a parent activity in the AndroidManifest.xml that the back arrow would lead to the parent; but this does not appear to be the case.
What do I need to do in the new activity (or in the original activity) to ensure that the back function works properly ?
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.x.y" >
<uses-permission android:name="android.permission.SET_DEBUG_APP" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Home"
android:exported="true"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ui.settings.FilterPreferences"
android:label="@string/title_activity_filter_preferences"
android:parentActivityName=".Home">
</activity>
</application>
</manifest>
Drawer Activity Home.java
New Activity FilterPreferences.java
public class FilterPreferences extends AppCompatActivity {
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter_preferences);
// Initialize the toolbar
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_filter_preferences, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The issue was that the calling activity was killing itself after launching the new activity.
private void displaySettingsPage() {
Intent i = new Intent(Home.this, FilterPreferences.class);
startActivity(i);
finish();
}
I would expect that the call to the back button would launch the Home activity again from onCreate(). This does not seem to occur - the parent activity must still be available to be referenced from the backstack.
The solution is to allow the system to handle the originating activity's lifecycle - the calling method then becomes:
private void displaySettingsPage() {
Intent i = new Intent(Home.this, FilterPreferences.class);
startActivity(i);
}