Android app calls MAIN/LAUNCHER again, instead of onResume()

久未见 提交于 2020-01-15 20:24:00

问题


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.



来源:https://stackoverflow.com/questions/8405827/android-app-calls-main-launcher-again-instead-of-onresume

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!