应用程序完全重新启动时启动的图标按下启动器

我正在尝试为我的第一个 Android 应用程序做一个发布版本,发送给一些测试人员。但是,我遇到了一个问题。当你退出应用程序,然后通过它的图标启动它重新进入它,它重新启动整个应用程序,而不是返回到它以前的位置。即使您在退出后立即重新进入,也会发生这种情况。但是,如果我按住 Home 按钮并通过最近的应用程序列表启动它,就不会发生这种情况。

我在网上搜索过其他有这个问题的人,有一些,但是没有人对为什么这个问题会发生在他们身上有一个确切的答案。在以前的问题中,有人建议在清单文件中将 launchmode 设置为 singletask 或 singleinstance,但这对我没有帮助,而且——据我所知,Android 的默认行为是在这种情况下返回到任务的先前状态,所以我不知道为什么我需要特殊的清单选项来做到这一点。

这个问题最奇怪的地方在于,如果我使用 eclipse 和调试器将应用程序放在手机上,这个问题就不会发生。我甚至不需要连接到调试器,似乎只要我有一个应用程序的调试版本,问题就不会发生。但是如果我使用一个发布版本(我使用 Eclipse 中的 Android Tools-Export Signed Application Package 菜单选项来创建它) ,就会出现问题。 如果有人知道是什么引起的,我想听听你们的想法。

33448 次浏览

When you press the back button in Android, the onDestroy method is invoked (as opposed to pressing the home button, where only the onPause() method is invoked).

If you need your app to continue where it left off, save the state of the app in your onDestroy() method and load that state in the onCreate() method.

It is the default behavior in Android. For the debug builds it works differently for some reason. It can be solved by adding android:launchMode="singleInstance" to the activity, you want to restart after you launch from the icon.

I had the same problem with an application and I resolved this behavior adding flag "android:launchMode="singleTop"" instead of "android:launchMode="singleTask"" in the <activity> declaration of your AndroidManifest.xml file. Hope this will help somebody.

Try using android:alwaysRetainTaskState as shown in the following example:

<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>

So far I've found out that it's an issue based on how you install it in your real device, specifically:

  1. If you simply copy and paste the APK to your device's local storage and install it from the device, regardless of whether it's signed or unsigned or taken from bin folder, it shows this behavior, app restarts from menu icon.

If you install it using one of the following options, This issue does not appear:

  1. Go to sdk/tools/ using a terminal or command prompt then type

    adb install <FILE PATH OF .APK FILE>
    

    In Linux, type:

    ./adb install <FILE PATH OF .APK FILE>
    
  2. Simply run your project from Eclipse.

I would be pleased to know if there's any possible way to distribute correct APKs for beta testing. I already tried exporting a signed APK because when you copy and paste an APK and install it manually it shows the rogue behavior.

Update:

I found out a solution. Follow these two Steps:

  1. Set android:launchMode="singleTask" = true for all activities of your app in the AndroidMainifest.xml inside the activity tag.
  2. Put this code in your Launcher Activity's onCreate().

    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;
    }
    }
    

This behavior is a bug in Android. Not a special case.

You could use launchMode as singleTop to the Launcher Activity in AndroidManifest.xml

       <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>

Add this to your first activity:

if (!isTaskRoot()) {
finish();
return;
}
super.onCreate(savedInstanceState);

All of the solutions above didn't work consistently on all of my devices. It worked on some Samsung but not all.

The cause of the problem for me was installing the APK manually.

For me the fix was adding LaunchMode = LaunchMode.SingleTop to my Activity attribute over the Main Activity:

/// <summary>
/// The main activity of the application.
/// </summary>
[Activity(Label = "SilhuettePhone",
Icon = "@drawable/icon",
Theme = "@style/MainTheme",
MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
ScreenOrientation = ScreenOrientation.Portrait,
LaunchMode = LaunchMode.SingleTop,
WindowSoftInputMode = SoftInput.AdjustResize)]

You can try to set android:alwaysRetainTaskState="true" for your launcher activity in AndroidManifest.xml.

    <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>

For details you can see https://developer.android.com/guide/topics/manifest/activity-element.html#always

 // 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;
}

write the above code in your launcher activity before calling setContentView. This will solve the problem

I see this issue on Android TV in 2019. Is there a better fix for it? other than

if (!isTaskRoot()) {
finish();
}

It works but looks like a hack more than the actual solution.

For me, I found that I had erroneously posted NoHistory = true in my activity attribute

[Activity(NoHistory = true, ScreenOrientation = ScreenOrientation.Landscape)]

This prevented the app resuming into this activity and restarted

I had a problem with a restarting app, my problem was in themes: I have differents fragments and I would have one background for all. But this cause a restarting app in some devices(.

I've deleted this line in themes and this helped:

item name ="android:windowBackground">@drawable/background /item

Removing task affinity rather than launch mode has worked somewhat for as it has its own demerits