I have a spinner that essentially starts multiple new activities based on onItemSelect();
However, what's happening on my app is if I start the same activity multiple times I have to hit the Android back button multiple times. How do I start the same activity and kill the previous one so that I don't have multiple layouts sitting there open?
Set android:noHistory=true for your <activity> in Manifest.xml. See here
Or, programatically:
Intent intent = new Intent(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Use the FLAG_ACTIVITY_CLEAR_TOP activity flag.
When you start a new activity you need to finish your old activity. So when you call your new intent:
startActivity(intent);
finish();
That will end the current activity.
You might want to consider setting the activity launchmode to "singletop" (this can be done in the Android Manifest). Then instead of creating a new activity, it will call OnNewIntent() of the existing activity.
Related
I have 3 activity . Activity A ,Activity B, Activity C.
This is the flow A->B->C.
Now i want to come to activity A from C .(i.e C->A) without getting its onCreate() called of Activity A.
so far my code is:But it will call onCreate() of ActivityA.
I want to call Restart().
Intent intent=new Intent(ActivityC.this,ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Two cases:
If you want to keep other activities live and just want to bring A to front, just use intent flag FLAG_ACTIVITY_REORDER_TO_FRONT.
If you don't want to clear all activities and want existing instance of A at top, then use intent flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP.
Note : If you use only FLAG_ACTIVITY_CLEAR_TOP, then onCreate will be called.
Keep android:launchMode="singleTask" in manifest at activity declaretion
An Activity with singleTask launchMode is allowed to have only one instance in the system
If instance already exists, i will not call 'onCreate' it will call 'onNewIntent' method.
See http://androidsrc.net/android-activity-launch-mode-example/ for better understand about launch modes.
Although setting the launch mode to singleTask will work, use of that launch mode is discouraged. The documentation for launch mode indicates singleTask is "not recommended for general use".
The desired behavior can be achieved using Intent flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP:
Intent intent=new Intent(ActivityC.this,ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish();
Activity A will not be recreated and will receive the intent via onNewIntent().
Use SingleTask launch mode for Activity A.
New intent will be delivered to existing instance.
I am trying to bring an activity to the front. I found many questions similar to this but none of them actually works.
I always hold a reference to the current activity in a variable in application class. While the application is running in the background (after onPause fires), if any message arrives, I need to bring the same activity to the front and display the message. The only way I got it worked is..
Intent i = new Intent(mCurrentActivity, JoboffersActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
The issue I have got with this, is that it recreates the activity which I wanted to avoid. I tried singleInstance and singleTask both in the manifest. If I do not add FLAG_ACTIVITY_CLEAR_TOP, on back key press it takes me to the previous instance of the same activity. Even if I add sigleInstance it creates two instances of the same activity. If I do not add FLAG_ACTIVITY_NEW_TASK then it shows an error Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
Try to add another flag FLAG_ACTIVITY_SINGLE_TOP to your intent.
According to Android documentation, combination of this 2 flags:
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
brings the current instance of Activity to the front.
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
There are a few times when the user presses the back button on my app, and unfortunately he doesn't get out; the activity slides, but behind it there is exactly the same one...How is that possible? How could I avoid it? should I implementate something for the onBackPressed() method?
Thanks for your advices.
You don't have to do implement onBackPressed. This sounds like multiple instances of the activity are being created/started, which is expected default behaviour when calling .startActivity() Check out the docs Tasks and Back Stack.
You could use singleTop as the launchmode or set the Intent.FLAG_ACTIVITY_SINGLE_TOP
on the intent that launches the activity.
Intent detailsIntent = new Intent(mContext, DetailsActivity.class);
detailsIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
startActivity(detailsIntent);
you can use android:launchMode="singleTop" your activity deceleration in the Manifest .
is it possible to start multiple activities at once? I mean, from main create 3 activities in some order and just the last will be visible? Up to now, I was able to create only one activity.
Thanks
You might need something like this in order to launch deep into the app after the user has clicked a notification in order to display some newly added content, for example.
Intent i = new Intent(this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
Intent j = new Intent(this, B.class);
j.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(j);
Intent k = new Intent(this, C.class);
startActivity(k);
In this way you can start activities A, B and C at the same time and suppress transitions to activities A and B. You get a single transition from your current activity to activity C. I strongly suggest that you log the Activity lifecycle method calls (onCreate etc.) to LogCat, for example. It helps a lot in understanding the order of events.
This can be a common thing to do in response to deep linking or other use cases where you, basically, need to synthetically rebuild the Task (and all the activities it should contain). Sometimes, just specifying parents in the manifest isn't enough.
Take a look at TaskStackBuilder. One common example:
TaskStackBuilder.create( context )
.addNextIntent( intentOnBottom )
// use this method if you want "intentOnTop" to have it's parent chain of activities added to the stack. Otherwise, more "addNextIntent" calls will do.
.addNextIntentWithParentStack( intentOnTop )
.startActivities();
Really old question but I thought I still answer it.
Use:
public void startActivities (Intent[] intents, Bundle options)
Try startActivity(new Intent(...); at the end of your onCreate-Method of the first Activity.
This will immediatly launch a new Activity and pause the first one.
With back-key you will get back to the last Activity
I have a splash screen activity, then a login activity. My history stack looks like:
SplashActivity
LoginActivity
when the user successfully logs in via LoginActivity, I want to start WelcomeActivity, but clear the entire stack:
SplashActivity
LoginActivity // launches WelcomeActivity ->
WelcomeActivity
// but now all three are in the history stack, while I only
// want WelcomeActivity in the stack at this point.
Is there some flag I can use to do that?
// LoginActivity.java
Intent intent = new Intent(this, WelcomeActivity.class);
intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
Not sure if using the FLAG_ACTIVITY_CLEAR_TASK will clear out all activities in my task or not. I can do this 'manually' by unwinding the stack by using startActivityForResult() calls, but will be more fragile and more code to maintain.
Thanks
Yes that should work fine. You could use:
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_NEW_TASK
which ensures that if an instance is already running and is not top then anything on top of it will be cleared and it will be used, instead of starting a new instance (this useful once you've gone Welcome activity -> Activity A and then you want to get back to Welcome from A, but the extra flags shouldn't affect your case above).
Intent intent = new Intent(this, NextActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Use android:noHistory="true" on the splash activity in the manifest file.
<activity
android:name=".activity.SplashActivity"
android:theme="#style/theme_noActionBar"
android:noHistory="true">
finish() removes the activity from the stack. So, if you start LoginActivity and call finish() on SplashActivity, and then you do exact the same to start WelcomeActivity you will get the desired behaviour. No need to use extra flags.
Intent.FLAG_ACTIVITY_NO_HISTORY can work in your case too if you do not want the activity on the history stack.
Just do this to clear all previous activity in a task:
finishAffinity() // if you are in fragment use activity.finishAffinity()
Intent intent = new Intent(this, DestActivity.class); // with all flags you want
startActivity(intent)
In case that all of three activity is involved in the same app(same taskAffinity), you can pick either 1,2 or 3 below. otherwise you should pick 1,2 below.
If you don't want to return back SplashActivity from LoginActivity, you can define activity attribute noHistory in AndroidManifest.xml or you can set FLAG_ACTIVITY_NO_HISTORY into the intent to launch SplashActivity. if SplashActivity is started from Launcher, you should pick way to set activity attribute noHistory.
If you don't want to return back LoginActivity from WelcomeActivity, you can use either activity attribute noHistory or FLAG_ACTIVITY_NO_HISTORY like number 1 above.
If you want to clear back stack on specific situation, you can use FLAG_ACTIVITY_CLEAR_TASK in conjunction with FLAG_ACTIVITY_NEW_TASK(FLAG_ACTIVITY_CLEAR_TASK always must be used in conjunction with FLAG_ACTIVITY_NEW_TASK). But, if the activity being started is involved in other app(i.e different taskAffinity), the task will be launched other task after the task is cleared, not current task. so make sure that the activity being launched is involved in the same app(taskAffinity).