App Completely Restarting When Launched by Icon Press in Launcher

App completely restarting when launched by icon press in launcher

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.

Android app restarts when opened by clicking app icon

I found it. I had set a flag android:launchMode="singleTask" in my activity flag. I deleted that code.

I also added onsaveInstance method to all the activities in my code and it's working now!

Thanks :)

Android app restarts when user clicks on its icon

It's all about IDE.
After closing an app which was started by IDE (eclipse or IDEA - doesn't matter) Android deletes all it's temporary data (don't ask me why)

So the solution is:

1) run app from ide (deploy it on device)
2) press back button to close an app
3) start an app again
...
and now it will resume working after quitting

App restarts rather than resumes

Aha! (tldr; See the statements in bold at the bottom)

I've found the problem... I think.

So, I'll start off with a supposition. When you press the launcher, it either starts the default Activity or, if a Task started by a previous launch is open, it brings it to the front. Put another way - If at any stage in your navigation you create a new Task and finish the old one, the launcher will now no longer resume your app.

If that supposition is true, I'm pretty sure that should be a bug, given that each Task is in the same process and is just as valid a resume candidate as the first one created?

My problem then, was fixed by removing these flags from a couple of Intents:

i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );

While it's quite obvious the FLAG_ACTIVITY_NEW_TASK creates a new Task, I didn't appreciate that the above supposition was in effect. I did consider this a culprit and removed it to test and I was still having a problem so I dismissed it. However, I still had the below conditions:

i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

My splash screen was starting the "main" Activity in my app using the above flag. Afterall, If I had "restart" my app and the Activity was still running, I would much rather preserve it's state information.

You'll notice in the documentation it makes no mention of starting a new Task:

If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.

For example, consider a task consisting of the activities: A, B, C, D.
If D calls startActivity() with an Intent that resolves to the
component of activity B, then C and D will be finished and B receive
the given Intent, resulting in the stack now being: A, B.

The currently running instance of activity B in the above example will
either receive the new intent you are starting here in its
onNewIntent() method, or be itself finished and restarted with the new
intent. If it has declared its launch mode to be "multiple" (the
default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same
intent, then it will be finished and re-created; for all other launch
modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be
delivered to the current instance's onNewIntent().

This launch mode can also be used to good effect in conjunction with
FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task,
it will bring any currently running instance of that task to the
foreground, and then clear it to its root state. This is especially
useful, for example, when launching an activity from the notification
manager.

So, I had the situation as described below:

  • A launched B with FLAG_ACTIVITY_CLEAR_TOP, A finishes.
  • B wishes to restart a service so sends the user to A which has the service restart logic
    and UI (No flags).
  • A launches B with FLAG_ACTIVITY_CLEAR_TOP, A finishes.

At this stage the second FLAG_ACTIVITY_CLEAR_TOP flag is restarting B which is in the task stack. I'm assuming this must destroy the Task and start a new one, causing my problem, which is a very difficult situation to spot if you ask me!

So, if all of my supposition are correct:

  • The Launcher only resumes the initially created Task
  • FLAG_ACTIVITY_CLEAR_TOP will, if it restarts the only remaining Activity, also recreate a new Task

Understanding Android LaunchMode - Prevent restarting on App Icon click

This somehow works:

<gap:config-file platform="android" parent="/manifest/application">
<activity android:launchMode="singleInstance" />
</gap:config-file>

App restarting on play store open click

I had same issue. Put below code to your launcher Activity.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& Intent.ACTION_MAIN.equals(getIntent().getAction())) {
finish();
return;
}
//other code
}

Problem:


Suppose you have task stack [A -> B -> C] and A is root Activity,
when you launch application from play store task stack will become(system clears the top of root) [A] but if you have finished your root Activity stack will look like(system will create new instance of root Activity and place top of the existing task) [B -> C -> A].

Why?

Because play store will launch app with category=LAUNCHER, action=MAIN, flag = ACTIVITY_NEW_TASK, flag = ACTIVITY_BROUGHT_TO_FRONT

Solution


Take a case that you have removed your root activity and now your stack is [B -> C], when you try to open app from play store your stack become [B -> C -> A]


now, A is not in root of the stack and has category = LAUNCHER and action = MAIN so, we can remove A using finish() and stack will remain same [B -> C]

*Note: Tested on android 7.0



Related Topics



Leave a reply



Submit