Up Navigation is not working

Refresh

November 2018

Views

849 time

2

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:

<activity
            android:name=".MainActivity"
            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=".DetailsPage"
            android:label="@string/title_activity_details_page"
            android:parentActivityName="com.collusion.serviceassistant.MainActivity" >>
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.collusion.srviceassistant.MainActivity"/>
        </activity>

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 :
            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.
                TaskStackBuilder.create(this)
                        // Add all of this activity's parents to the back stack
                        .addNextIntentWithParentStack(upIntent)
                                // Navigate up to the closest parent
                        .startActivities();
            } 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

1

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

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

@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);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

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