Reputation: 15492
I have a service that starts an activity (say Activity A
) as follows:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
getApplicationContext().startActivity(intent);
when I start my app, Activity A
appears instead before the app's first activity.
I had 2 solutions to work this around:
1)
changing the above code to:
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
problem:
after launching the activity and pressing home-key, I still see the activity in the history stack (=recent tasks
). how come?
after I launch my app, the two tasks merge into one with the app's first activity at the top. but any other app, create a new task and the task of Activity A
remains. how come?
2) changing the above code to:
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
and adding to the manifest:
<activity
android:name="org.achartengine.GraphicalActivity"
android:taskAffinity="org.achartengine"
>
where Activity A
= android:name="org.achartengine.GraphicalActivity"
problem: I keep unnecessary task stack.
I assume few people start that activity from the service. And even then they will do it only once. Meaning it would be more neat to clean the activity from any history stack when the user clicks home/back key.
which of the options is preferred? (or another one?)
Upvotes: 0
Views: 3284
Reputation: 3425
This is a method that will keep the first activity off your stack.
Thread nohistory = new Thread () {
public void run () {
try {
Intent i = new Intent(Splash.this, ActivitySplash.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
};
nohistory.start();
Upvotes: 0
Reputation: 3425
A quick fix is adding android:launchMode="singleTop" to the activity in the manifest, that way there will only be one and not multiple instances. The other way to accomplish the same thing is to use the following just prior to starting the intent.
Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
There are a couple of other possibilities to try in the manifest put
android:noHistory="true"
and make sure your are calling
finish();
to end that intent.
for the activity or a combination of these 2 intent flags
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP
If the above doesn't work, the last thing I can think of is
moveTaskToBack(true);
which doesn't solve the problem.
Hope this helps.
Upvotes: 1