Android app calls MAIN/LAUNCHER again, instead of

2019-07-31 23:59发布

问题:

when my android app - Activity B - is moved to the background (by the user pressing the android home button), it is still alive, onStop() was called as expected.

Activity B has not been destroyed here, but when the user clicks on the app's icon again, it calls the intent-filter MAIN and LAUNCHER again, starting Activity A, instead of calling onResume() on Activity B.

so the launch activity is shown - Activity A - , but if the user presses android back button, then launch activity finishes() , and the old activity - Activity B - is shown!

what should happen when Activity B is moved to the background is that Activity B is paused and stopped. When the app is resumed by pressing the app icon, Activity B should be resumed. (unless it was killed and destroyed, which it hasn't been)

How do I fix this? Activity B is originally opened by Activity A using

Intent next = new Intent(ActivityA.this, ActivityB.class);
startActivity(next);
finish();

Here is the manifest entry for Activity B

<activity android:name=".ActivityB"
    android:screenOrientation="portrait"
              android:label="@string/app_name"
              android:configChanges="orientation|keyboardHidden">
</activity>

Here is the manifest entry for Activity A

<activity android:name=".Activity A"
     android:screenOrientation="portrait"
              android:label="@string/app_name"
              android:configChanges="orientation|keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>

why is this happening and how do I fix it?

回答1:

Use android:launchMode="singleTask" in your manifeast file where you declare activity b. Then activity b not display in background.