Create a New Activity With Navigation Android
Create a New Activity With Up Navigation#
Prefer Android Studio’s activity templates for new screens.
If you are wiring an activity manually, declare the parent activity in AndroidManifest.xml and handle the Up button from the app bar.
Start the Activity#
public void goToNewActivity(View view) {
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
}
Declare the Parent Activity#
<activity
android:name=".NewActivity"
android:parentActivityName=".MainActivity" />
Enable the Up Button#
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Then handle the selected item:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
navigateUpTo(new Intent(this, MainActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
For new apps with multiple screens, prefer the Jetpack Navigation component instead of manually managing every Up action.