Android 向上按钮退出应用程序而不是转到父级

Android Up button quitting application instead of going to parent

所以在我的应用程序中,当我按下向上按钮时,activity 退出而不是转到其父 Activity。这里可能出了什么问题?

SettingsActivity.java:

public class SettingsActivity extends AppCompatPreferenceActivity {

private Toolbar mToolbar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
    LinearLayout content = (LinearLayout) root.getChildAt(0);
    LinearLayout toolbarContainer = (LinearLayout) View.inflate(this, R.layout.activity_settings, null);

    root.removeAllViews();
    toolbarContainer.addView(content);
    root.addView(toolbarContainer);

    mToolbar = (Toolbar) toolbarContainer.findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        // Show the Up button in the action bar.
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    addPreferencesFromResource(R.xml.pref_general);

}


@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        onBackPressed();
        return true;
    }
    return super.onMenuItemSelected(featureId, item);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    NavUtils.navigateUpFromSameTask(this);

}
}

如何称呼这个 activity:

private void openSettingsActivity() {
    Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
    startActivity(intent);
}

和 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pt.bluecover.wearable3d"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="23" />

<android:uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
<android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/female_icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".DrawerActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SettingsActivity"
        android:label="@string/title_activity_settings"
        android:parentActivityName=".DrawerActivity"
        android:screenOrientation="portrait">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="pt.bluecover.wearable3d.DrawerActivity" />
    </activity>
</application>
</manifest>

那么,这种行为可能有什么问题?

试试这个代码

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
    onBackPressed();
    return true;
}
return super.onMenuItemSelected(featureId, item);
}


@Override
public void onBackPressed() {
super.onBackPressed();
//NavUtils.navigateUpFromSameTask(this);
finish();
}

将此添加到您的 SettingsActivity

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    if (menuItem.getItemId() == android.R.id.home) {
        onBackPressed();
    }
    return super.onOptionsItemSelected(menuItem);
}

不要忘记覆盖 onBackPressed 并调用 super.onBackPressed();

@Override
public void onBackPressed() {
super.onBackPressed();
}