没有将DrawerLayout添加到后台堆栈的活动

时间:2015-10-27 12:20:41

标签: android navigation-drawer material-design android-toolbar back-stack

我有一个使用Android DrawerLayout和NavigationView的应用程序来提供抽屉。

抽屉效果很好,菜单项也有效。当我单击启动新活动的菜单项时,活动会呈现,工具栏会显示一个反向行。

但是,当我点击新活动的后退箭头时,我不会使用DrawerLayout返回原始活动。相反,我完全退出了应用程序!

我原以为如果我在AndroidManifest.xml中指定了一个父活动,后面的箭头会指向父项;但事实似乎并非如此。

我需要在新活动(或原始活动)中做些什么来确保后退功能正常工作?

的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.x.y" >

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Home"
            android:exported="true"
            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=".ui.settings.FilterPreferences"
            android:label="@string/title_activity_filter_preferences"
            android:parentActivityName=".Home">
        </activity>
    </application>

</manifest>

抽屉活动 Home.java

新活动 FilterPreferences.java

public class FilterPreferences extends AppCompatActivity {

    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter_preferences);

        // Initialize the toolbar
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_filter_preferences, menu);
        return true;
    }

    @Override
    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.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

问题是调用活动在启动新活动后自行终止。

   private void displaySettingsPage() {
        Intent i = new Intent(Home.this, FilterPreferences.class);
        startActivity(i);
        finish();
    }

我希望调用后退按钮可以从onCreate()再次启动Home活动。这似乎不会发生 - 父活动必须仍然可以从后台堆栈中引用。

解决方案是允许系统处理原始活动的生命周期 - 然后调用方法变为:

   private void displaySettingsPage() {
        Intent i = new Intent(Home.this, FilterPreferences.class);
        startActivity(i);
    }
相关问题