What the way to manage multiple instances of Android application? I want to close the ruuning application's instance when user launch the same application from Application menu.
Regards,
Android_IT
This is what I use to clear the backstack and start from the beginning if that is what you're looking for
Intent intent = new Intent(this, Main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Related
I have used finishAffinity or below code because of finish all activities. But my application removed recent applications.
I want to finish all activities and keep appearing in recent applications. How can I do that?
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
System.exit(0);
It is used to remove a number of Activitys belonging to a specific
application from the current task (which may contain Activitys
belonging to multiple applications)
Try this:
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I have a PreferenceActivity with a custom DialogFragment for clearing the app data, so I want it when the user clicks Yes, the application to either close completely (finish all activities) or just to tell it to go to the InitialSetup activity, so the app ca be set up again anew. So far I havent been able to do it in any way...
Tried with
Intent intent = new Intent(context.getApplicationContext(), MySettings.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent);
but that still does not close all the activities in the back stack...
How can I do it?
What you need is FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flags for your intent:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Or as mentioned Sartheris use IntentCompat for Android APIs below 11:
Intent intent = IntentCompat.makeRestartActivityTask(new ComponentName(context, MySettings.class));
startActivity(intent);
If you only want the app to close, with the user having to start it again later, you can use
System.exit(0);
as you would in ordinary Java.
If you want your app to restart automatically after it's closed, use a PendingIntent as the answer for this question does:
how to programmatically "restart" android app?
I have 4 Activities 1.Home,2.B,3.C and 4.D. Whenever I start Home from Activity D I want to finish all other activities. I Used this code, but when I press back button from Home it brings me to the previous activity. What I did wrong here.?
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent)
You can try this,
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Note: As described in FLAG_ACTIVITY_CLEAR_TOP documentation
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.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
This will work only for activities which are still in the activity stack. I believe you are finishing the Home Activity when going to B. So that CLEARTOP won't work.
Now try something like this.
You need to set an Extra with intent Of "D" to Home. Then you have to check the Intent extra in Home, call finish() if the extra matching
Intent intent = new Intent(contxt, Home.class);
intent.putExtra("urString",defaultvalue);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
// Checking the intent extra at "HOME"
if(getIntent().hasExtra("urString")){
// manage your own way
finish();
}
In manifest.xml file set android:nohistroy="true" for all the activities
I would like to finish all the activities in an Android app and then restart it from the beginning. Is there an easy way to achieve this without finishing one by one until I come back to the first one?
Just clear the task when launching your intent.
Intent restartIntent = new Intent(this, MainActivity.class);
restartIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //Set this flag
startActivity(restartIntent);
finish();
i am launching another application through my application,so i want to check is any instance of that application is already in stack or not? if exist then clear all instance before launch.
Would you be looking for the following flags to add to the Intent you start the other application with:
FLAG_ACTIVITY_CLEAR_TASK
Try adding this flags to your intent
Intent intent= getPackageManager().getLaunchIntentForPackage("com.package.address");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);