Up Navigation for Activities

suggest change

Up navigation is done in Android by adding android:parentActivityName to the activity tag in the manifest. With this attribute you tell the system the parent activity of an activity. android:parentActivityName was added in API 16.

How is it done?

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:name=".SkillSchoolApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".ui.activities.SplashActivity"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ui.activities.MainActivity" />
    <activity
        android:name=".ui.activities.HomeActivity"
        android:parentActivityName=".ui.activities.MainActivity" />
    <!-- MainActivity is the parent of HomeActivity -->
</application>

Now when I click the Up arrow in the toolbar of HomeActivity it will take me back to the parent activity.

Java Code

Here is the Java code required for this functionality.

public class HomeActivity extends AppCompatActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        // Since I am using a custom toolbar, I set that toolbar as the action bar. If you are not using a custom toolbar, you can leave this out and move to the next line.
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true); // this will show the Up arrow in the toolbar.
    }
}

If you run this code, pressing the Up arrow takes you back to MainActivity. For further understanding of Up navigation, I would recommend reading the docs.

You can customize this behaviour further by overriding

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this); // Here you will write your logic for handling up navigation
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Simple Hack

This is simple hack which is mostly used to navigate to parent activity if parent is in backstack. By calling onBackPressed() if id is equal to android.R.id.home

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents