Reputation: 1136
I'm in the process of trying to make a release build of my first android app to send to a few testers. However, I ran into a problem with it. When you exit the app and then re-enter it by launching it via its icon, it restarts the whole app instead of returning to it's previous location. This occurs even if you re-enter right after exiting. However, it does not happen if I hold the Home button and launch it through the recent apps list.
I've searched online for others having this problem and there are a few, but no one has ever had a solid answer as to why it's happening to them. It's been suggested in old questions to set the launchmode to singletask or singleinstance in the manifest file, but that hasn't helped me, and besides - from what I understand, the default behavior for android is to return to the previous state of the task in this situation, so I don't know why I would need special manifest options to make it do that.
The most bizarre thing about this problem is that if I use eclipse and the debugger to put the app on my phone, this problem does not occur. I don't even need to be connected to the debugger, it seems like as long as I have a debug version of the app, the problem doesn't occur. But if I use a release version (I create it using the Android Tools - Export Signed Application Package menu option in Eclipse), the problem happens. If anyone has any insight as to what is causing this, I'd love to hear your thoughts.
Upvotes: 92
Views: 36421
Reputation: 1065
Removing task affinity rather than launch mode has worked somewhat for as it has its own demerits
Upvotes: 0
Reputation: 161
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
Upvotes: 0
Reputation: 299
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
Upvotes: 3
Reputation: 3712
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.
Upvotes: 2
Reputation: 2699
// 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
Upvotes: 12
Reputation: 686
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>
Upvotes: 2
Reputation: 31
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
Upvotes: 3
Reputation: 1433
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)]
Upvotes: 0
Reputation: 1261
So far I've found out that it's an issue based on how you install it in your real device, specifically:
If you install it using one of the following options, This issue does not appear:
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>
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:
android:launchMode="singleTask" = true
for all activities of your app in the AndroidMainifest.xml inside the activity tag.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.
Upvotes: 34
Reputation: 1760
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.
Upvotes: 0
Reputation: 682
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.
Upvotes: 65
Reputation: 4073
Add this to your first activity:
if (!isTaskRoot()) {
finish();
return;
}
super.onCreate(savedInstanceState);
Upvotes: 4
Reputation: 715
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>
Upvotes: 8
Reputation: 49
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.
Upvotes: 4
Reputation: 22166
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.
Upvotes: -1