我们已经检查过$( ".present a" ).click(function() {
$.cookie($(this).attr('href'), true);
});
和CATEGORY_MAIN
,但即使这样,也会启动2个活动实例。
SplashActivity.java
!isTaskRoot()
日志
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("OnCreate method.");
if(checkIfActivityIsBroughtToFront() || checkIfActivityIsRootTask()) {
return; // Found that if we finish and don't return then it will run the code below, hence start the recovery task.
}
Log.i("Checking if Recovery is required ...");
new RecoveryTask(SplashActivity.this, this).execute();
}
private boolean checkIfActivityIsBroughtToFront() {
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
// Activity was brought to front and not created,
// Thus finishing this will get us to the last viewed activity
Log.i("Detecting a brought to front, no need for recovery.");
finish();
return true;
}
return false;
}
private boolean checkIfActivityIsRootTask() {
if (!isTaskRoot()) {
final Intent intent = getIntent();
final String intentAction = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
Log.i("Main Activity is not the root. " + "Finishing Main Activity instead of launching.");
finish();
return true;
}
}
return false;
}
清单
2015-10-22 13:42:25.581 +0300 SplashActivity INFO[main] - OnCreate method.
2015-10-22 13:42:25.587 +0300 SplashActivity INFO[main] - Checking if Recovery is required ...
2015-10-22 13:42:25.637 +0300 SplashActivity INFO[main] - OnCreate method.
2015-10-22 13:42:25.638 +0300 SplashActivity INFO[main] - Checking if Recovery is required ...
2015-10-22 13:42:25.828 +0300 GeoFenceManager INFO[pool-5-thread-1] - Removing geofences ...
2015-10-22 13:42:25.872 +0300 GeoFenceManager INFO[pool-5-thread-2] - Removing geofences ...
更新: 这是在重启后发生的,BOOT_COMPLETED监听器正在跟随
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="x.y.z"
android:installLocation="internalOnly" >
<application
android:name=".global.GlobalInstance"
android:allowBackup="true"
android:allowClearUserData="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:persistent="true" >
<activity
android:name=".activity.SplashActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/Theme.Background" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".receiver.BootUpReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
知道如何预防这种情况吗?
答案 0 :(得分:1)
将android:launchMode =“SingleTask”添加到xml中的splash活动元素。然后离开活动,即远离泼水通话结束()。您应该使用此模式而不是“SingleInstance”的原因是用户永远不能使用反键导航回到启动画面(因为这不是正常行为)。
答案 1 :(得分:0)
private boolean checkIfActivityIsRootTask() {
if (!isTaskRoot()) {
final Intent intent = getIntent();
final String intentAction = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
Log.i("Main Activity is not the root. " + "Finishing Main Activity instead of launching.");
finish();
return true;
}
}
return false;
} ?
如果此活动是roottask,则返回false; 什么是RecoveryTask?
答案 2 :(得分:0)
你说:
我们已经检查过CATEGORY_MAIN和!isTaskRoot()
但是检查都失败了。在您附加的日志中,创建两个实例时,日志中不会打印Detecting a brought to front, no need for recovery.
和Main Activity is not the root. " + "Finishing Main Activity instead of launching.
。这意味着在两种方法中都不会遇到if
部分,因此永远不会执行finish()
。这两个方法都返回false。
另外,仅仅因为您在finish()
中呼叫return
和onCreate()
,并不意味着不会调用onStart()
或onResume()
。
为什么会发生这种情况? Launcher活动永远不会被调用两次(考虑到你没有在内部启动活动)。可能是用户拥有的某些SDK中的错误。
可能的修正:
您可以尝试在android:launchMode="singleTop"
的清单中设置SplashActivity
。这将确保只维护一个实例。