我正在尝试将我的第一个Android应用程序的发布版本发送给几个测试人员。但是,我遇到了问题。当您退出应用程序,然后通过其图标启动它重新输入它时,它会重新启动整个应用程序,而不是返回到它以前的位置。即使您在退出后立即重新输入,也会发生这种情况。但是,如果我按住主页按钮并通过最近的应用列表启动它,则不会发生这种情况。
我在网上搜索了其他有这个问题的人,但也有一些,但没有人对他们为什么会这样做有一个可靠的答案。在旧问题中建议将启动模式设置为清单文件中的singletask或singleinstance,但这对我没有帮助,而且 - 根据我的理解,android的默认行为是返回到任务的先前状态在这种情况下,所以我不知道为什么我需要特殊的清单选项才能做到这一点。
关于这个问题最奇怪的是,如果我使用eclipse和调试器将应用程序放在我的手机上,则不会出现此问题。我甚至不需要连接到调试器,似乎只要我有应用程序的调试版本,问题就不会发生。但是,如果我使用发布版本(我使用Eclipse中的Android工具 - 导出签名应用程序包菜单选项创建它),则会出现问题。如果有人对导致这种情况有什么了解,我很乐意听到你的想法。
我在应用程序中遇到了同样的问题,我解决了这个问题,在AndroidManifest.xml文件的"android:launchMode="singleTop""
声明中添加了标志"android:launchMode="singleTask""
而不是<activity>
。希望这会对某人有所帮助。
将此添加到您的第一个活动:
onDestroy()
上述所有解决方案都无法在我的所有设备上保持一致。它适用于一些三星但不是全部。
问题的原因是我手动安装APK。
对我来说,修复是在主活动上将onCreate()
添加到我的Activity属性:
if (!isTaskRoot()) {
finish();
return;
}
super.onCreate(savedInstanceState);
到目前为止,我发现根据您在真实设备中的安装方式存在问题,具体如下:
如果使用以下选项之一安装它,则不会出现此问题:
adb install <FILE PATH OF .APK FILE>
在Linux中,键入:
./adb install <FILE PATH OF .APK FILE>
我很高兴知道是否有任何可能的方法来分发正确的APK进行beta测试。我已经尝试导出已签名的APK,因为当您复制并粘贴APK并手动安装时,它会显示恶意行为。
更新:
我找到了解决方案。请遵循以下两个步骤:
android:launchMode="singleTask" = true
。此行为是Android中的错误。不是特例。
您可以将launchMode作为singleTop用于AndroidManifest.xml中的Launcher Activity
onCreate()
另一个奇怪的原因,只有在复制到设备 - & - 安装后单击“打开”启动应用程序时才会重新启动。
测试OS8.1,活动中没有launchMode。
if (!isTaskRoot())
{
final Intent intent = getIntent();
final String intentAction = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
finish();
return;
}
}
这是Android中的默认行为。对于调试版本,由于某种原因它的工作方式不同。可以通过向活动添加 <activity
android:name="<YOUR_ACTIVITY>"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
来解决,您希望在从图标启动后重新启动。
您可以尝试在AndroidManifest.xml中为您的启动器活动设置android:launchMode="singleInstance"
。
// To prevent launching another instance of app on clicking app icon
if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
有关详细信息,请参阅android:alwaysRetainTaskState="true"
尝试使用 <activity
android:name=".YourMainActivity"
android:alwaysRetainTaskState="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
,如以下示例所示:
https://developer.android.com/guide/topics/manifest/activity-element.html#always
当您在Android中按后退按钮时,将调用android:alwaysRetainTaskState
方法(而不是按下主页按钮,其中仅调用<activity
android:name="com.jsnider.timelineplanner.MainActivity"
android:alwaysRetainTaskState="true"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
方法)。
如果您需要应用程序继续停止,请在onDestroy
方法中保存应用程序的状态,并在onPause()
方法中加载该状态。