因此,在我的应用程序中,当我按下向上按钮时,活动将退出而不是转到其父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);

}
}


此活动的调用方式:

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>


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

最佳答案

将此添加到您的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();
}

10-02 22:11