在childe活动开始时,父活动总是被销毁

时间:2016-08-09 15:12:29

标签: android android-activity android-manifest

我遇到了一个令我难以置信的问题。我正在开发的父母活动在任何一个孩子被启动时总是被销毁。这使得使用后退按钮会导致应用程序滞后,因为必须再次创建父活动。 Heres Android manifest.xml:

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <permission
        android:name="com.dev.dita.daystarmemo.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.dev.dita.daystarmemo.permission.C2D_MESSAGE" />

    <application
        android:name=".MemoApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_memo_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <meta-data
            android:name="io.fabric.ApiKey"
            android:value="e76f495714db69684d45e3cb181116b8c21b649e" />

        <activity
            android:name=".ui.welcome.WelcomeActivity"
            android:label="@string/title_activity_welcome"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".ui.main.MainActivity"
            android:label="@string/title_activity_main"
            android:launchMode="singleTask"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter android:label="@string/app_name">
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ui.profile.ProfileActivity"
            android:label="@string/title_activity_profile"
            android:parentActivityName=".ui.main.MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.dev.dita.daystarmemo.ui.main.MainActivity" />
        </activity>
        <activity
            android:name=".ui.memos.MemosChatActivity"
            android:label="@string/title_activity_memos_chat"
            android:parentActivityName=".ui.memos.MemoGroupActivity"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden|adjustResize">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.dev.dita.daystarmemo.ui.memos.MemoGroupActivity" />
        </activity>
        <activity
            android:name=".ui.memos.NewMemoActivity"
            android:label="@string/title_activity_new_memo"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateAlwaysVisible|adjustResize" />
        <activity
            android:name=".ui.connections.ConnectionsActivity"
            android:label="@string/title_activity_connections"
            android:launchMode="singleTop"
            android:parentActivityName=".ui.main.MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.dev.dita.daystarmemo.ui.main.MainActivity" />
        </activity>
        <activity
            android:name=".ui.connections.NewConnectionActivity"
            android:label="@string/title_activity_new_connection"
            android:parentActivityName=".ui.connections.ConnectionsActivity"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden|adjustResize">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.dev.dita.daystarmemo.ui.connections.ConnectionsActivity" />
        </activity>
        <activity
            android:name=".ui.connections.ConnectionDetailsActivity"
            android:label="@string/title_activity_connection_details"
            android:parentActivityName=".ui.connections.ConnectionsActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.dev.dita.daystarmemo.ui.connections.ConnectionsActivity" />
        </activity>
        <activity
            android:name=".ui.settings.SettingsActivity"
            android:label="@string/title_activity_settings"
            android:parentActivityName=".ui.main.MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.dev.dita.daystarmemo.ui.main.MainActivity" />
        </activity>

        <receiver
            android:name=".receivers.NotificationRemovedReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.dev.dita.daystarmemo.BROADCAST_NOTIFICATION_REMOVED" />
            </intent-filter>
        </receiver>
        <receiver
            android:name=".receivers.PushReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="0">
                <action android:name="com.dev.dita.daystarmemo.BROADCAST_NOTIFICATION" />
            </intent-filter>
        </receiver>
        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="com.dev.dita.daystarmemo" />
            </intent-filter>
        </receiver>

        <service
            android:name=".services.GcmService"
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:name=".services.MemoService"
            android:exported="false" />
        <service
            android:name=".services.PushReceiverIntentService"
            android:exported="false" />
        <service
            android:name=".services.PushIntentService"
            android:exported="false" />
        <service
            android:name=".services.MemoPendingDownloadService"
            android:exported="false" />
        <service
            android:name=".services.ConnectionService"
            android:enabled="true"
            android:exported="false" />
        <service
            android:name=".services.RegistrationService"
            android:exported="false" />

        <activity
            android:name=".ui.documents.DocumentsActivity"
            android:label="@string/title_activity_documents"
            android:parentActivityName=".ui.main.MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.dev.dita.daystarmemo.ui.main.MainActivity" />
        </activity>

        <service
            android:name=".services.TransfersService"
            android:enabled="true"
            android:exported="false" />

        <activity
            android:name=".ui.memos.MemoGroupActivity"
            android:label="@string/title_activity_memo_group"
            android:parentActivityName=".ui.main.MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.dev.dita.daystarmemo.ui.main.MainActivity" />
        </activity>
        <activity android:name=".ui.SearchableActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
        <activity
            android:name=".ui.schedule.ScheduleActivity"
            android:label="@string/title_activity_schedule"
            android:parentActivityName=".ui.main.MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.dev.dita.daystarmemo.ui.main.MainActivity" />
        </activity>

        <service
            android:name=".services.ScheduleService"
            android:enabled="true"
            android:exported="false"></service>
    </application>

</manifest>

我的后退按钮实现类似于Google IO 2015应用。在开始任何活动时我都不使用任何标志,因此我认为这不是问题。

这就是我发布活动的方式:

private void goToNavDrawerItem(int item) {
        Intent intent;
        switch (item) {
            case NAVDRAWER_ITEM_HOME:
                startActivity(new Intent(this, MainActivity.class));
                break;
            case NAVDRAWER_ITEM_MEMOS:
                intent = new Intent(this, MemoGroupActivity.class);
                intent.putExtra("section", MemoGroupActivity.MEMO);
                createBackStack(intent);
                break;
            case NAVDRAWER_ITEM_GROUPS:
                intent = new Intent(this, MemoGroupActivity.class);
                intent.putExtra("section", MemoGroupActivity.GROUP);
                createBackStack(intent);
                break;
            case NAVDRAWER_ITEM_CONNECTIONS:
                createBackStack(new Intent(this, ConnectionsActivity.class));
                break;
            case NAVDRAWER_ITEM_SCHEDULE:
                createBackStack(new Intent(this, ScheduleActivity.class));
                break;
            case NAVDRAWER_ITEM_DOCUMENTS:
                createBackStack(new Intent(this, DocumentsActivity.class));
                break;
            case NAVDRAWER_ITEM_SETTINGS:
                createBackStack(new Intent(this, SettingsActivity.class));
                break;
            case NAVDRAWER_ITEM_LOGOUT:
                showLogoutDialog();
                break;
        }
    }

继承创建后台堆栈方法

private void createBackStack(Intent intent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            TaskStackBuilder builder = TaskStackBuilder.create(this);
            builder.addNextIntentWithParentStack(intent);
            builder.startActivities();
        } else {
            startActivity(intent);
            finish();
        }
    }

1 个答案:

答案 0 :(得分:1)

请检查您是否已选中设置&gt;开发人员选项下的“请勿保留活动”选项。 如果选中它,则在启动新活动时将销毁所有活动。