Up Navigation is not working


November 2018


849 time


So I have a very simple project. It contains two activities. The main activity has a navigation drawer and a fragment container. The second activity is merely meant to display details when the user interacts with a certain fragment.

So I have set my main activity as the parent activity to my second activity (called DetailsPage) like this:

            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            android:parentActivityName="com.collusion.serviceassistant.MainActivity" >>

and in the DetailsPage activity code I have the following:

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.
    switch (item.getItemId()) {
        case R.id.share :
            return true;

        case R.id.home:
            Log.i("BACK", "Going back!");
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                // This activity is NOT part of this app's task, so create a new
                // task
                // when navigating up, with a synthesized back stack.
                        // Add all of this activity's parents to the back stack
                                // Navigate up to the closest parent
            } else {
                // This activity is part of this app's task, so simply
                // navigate up to the logical parent activity.
                NavUtils.navigateUpTo(this, upIntent);
            return true;
    return super.onOptionsItemSelected(item);

Now it seems that by my log, the code that pertains to the up action is not getting executed. Whenever I hit the up caret in the action bar or use the back hardware key, the app simply exits. Does anyone have any idea what I am doing wrong here? I am wondering if it has anything to do with the fact that the details page activity does not extend the ActionBar class:

public class DetailsPage extends Activity{

Does anyone have any ideas?

1 answers


Согласно документации Google по ссылке навигации с помощью кнопки домой , вы можете использовать

android.R.id.home вместо R.id.home

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        return true;
    return super.onOptionsItemSelected(item);

Я думаю, что это может решить домашнюю навигацию.