假设我有一个简单的应用程序,有a SplashScreenActivity和a MainActivity.
以下是我的一部分AndroidManifest.xml:
<activity
android:name=".front.activities.splashscreen.SplashScreenActivity"
android:launchMode="standard"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".front.activities.main.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/Main_Title"
android:launchMode="singleTop"
android:screenOrientation="portrait"/>
Run Code Online (Sandbox Code Playgroud)
我SplashScreenActivity打开MainActivity了onCreate().
如果应用程序是从Google Play而不是启动器启动的,如果我按下主页并单击启动器中的应用程序图标,SplashScreenActivity则会再次启动一个应用程序图标,因此会MainActivity在后端堆栈上再启动一个应用程序.
SplashScreenActivity已经重新启动的通知(或者通过查看日志)SplashScreenActivity并MainActivity启动.经过几次试验,如果我们按下后退按钮,我们会注意到MainActivity后面的堆栈中有多个.
SplashScreenActivity则会启动2 .但是,如果我再次从启动器按下应用程序图标,它将不会创建第3个SplashScreenActivity.它将首次推出MainActivity顶级产品.SplashScreenActivity给android:launchMode="singleTask".没有帮助.MainActivity给android:launchMode="singleTask".这将防止MainActivity创建多个,但不能解决SplashScreenActivity多次启动的问题.您也可以假设MainActivity不应该设置为singleTask.通过点击启动器中的应用程序图标,Android应该能够在我的任务管理器中找到该应用程序,该应用程序已经启动并且只是将其置于顶部.
我怎样才能解决这个问题?
某些启动器存在此错误:当应用程序从主屏幕启动时,会创建初始活动的新实例,而不是恢复应用程序。可以通过添加来修复
if (!isTaskRoot()) {
finish();
return;
}
Run Code Online (Sandbox Code Playgroud)
到onCreate()初始活动。也可以看看:
恢复顶级 Activity 而不是启动启动器 Activity
| 归档时间: |
|
| 查看次数: |
157 次 |
| 最近记录: |