How to close an android app if more than one activity is in active state?
A blog post entitled Exiting Android Application will show how to exit an Android app:
When the user wishes to exit all open activities, they should press a button which loads the first Activity that runs when your app starts, in my case "LoginActivity".
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
The above code clears all the activities except for LoginActivity. LoginActivity is the first activity that is brought up when the user runs the program. Then put this code inside the LoginActivity's onCreate, to signal when it should self destruct when the 'Exit' message is passed.
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
I got an easy solution for this problem
From the activity you press the exit button go to the first activity using the following source code. Please read the documentation for FLAG_ACTIVITY_CLEAR_TOP also.
Intent intent = new Intent(ExitConfirmationActivity.this, FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Now overide onResume() of the first activity using finish()
The answer is simple: You really do not need to 'close' an Android application. If no activity is shown any more, the system will kill the process after some time. The users can close activities by pressing the 'back' button. Reto Meier explains it pretty well here:
http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html
You might also want to read this thread; it is very helpful to say the least: Quitting an Android application - Is it frowned upon?
Well, you shouldn't close your applications, as the system manages that. Refer to the posts/topics in the other answers for more information.
However, if you really, really want to, you can still call System.exit (0); like in any other Java application.
EDIT
ActivityManager actmgr = (ActivityManager) this.getSystemService (Context.ACTIVITY_SERVICE);
actmgr.restartPackage ("com.android.your.package.name");
I remembered something. I was trying to use this code to restart my application, but it only managed to kill my app. You can try it and see if it works for you.
I asked a similar question a couple of weeks back. Do go through the answers and comments for more perspective and possible solutions.
IMO quitting an application depends on what your application does and the user expectations. While I understand the rationale on not having a quit button I also do believe that it's a choice that the application designer has to make based on the situation.
Once your last Activity looses focus, Android will unload your process according to the current system needs / free resources.
You shouldn't really care about that - just use the lifecycle onStart, OnStop etc... to manage your state.
If you want to exit from one Android activity, this will bring you back to the previous activity or another activity from a specific place in current activity.
finish();
System.exit(0);
Related
This question already has answers here:
Finish all activities at a time
(21 answers)
Closed 3 years ago.
I want to kill my app from a exit button on my navigation drawer. the drawer has a exit button that is suppose to just kill the whole app (all activity) and gets the user out of the app .
When i am using finish() in the first activity its working but when i go into the app and call finish() the activity gets killed and returns to previous activity but never really kills the app.
Tried to use System.exit(0) but no luck.
Extra Information Might Help
I have all my activity started with android:launchMode="singleTop" . it means all those activities that are already created will not be created again and just reordered to front on calling.
Do anyone have any suggestion for this , please do help.
Update
I want to make some updates here as my question looks like this SO Question.
As i have already said i am using a android:launchMode="singleTop" . and this answer is not working for my case. I have to call onCreate() to make this happen but it is not my case.
Use below code
public void AppExit()
{
this.finish();
Intent intent= new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
Call above function to exit from App.
First, you need to decide if you just need to take the user out of the app, or if you also need to finish() all of your activities. It depends on your needs, but my guess is that in most cases most users won't know the difference, and Android is designed to have many apps having activities still running. If this is all you need, then you can just use an Intent to take the user to the home screen.
Intent intent = new Intent(Intent.
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
If you actually want to finish() your activities, then you need a mechanism to inform all currently running activities that the user is exiting the app. You can use something like EventBus to send an event that will be received by all registered subscribers; if you don't want to use a third-party library, you can do the same type of thing with LocalBroadcastManager. To do that, you would register a BroadcastReceiver with an IntentFilter for a custom action and broadcast an Intent with that action when the user wants to exit.
In either case, every activity should receive the signal and call finish() on itself. Note that the subscription (in the EventBus case) or the registration of a receiver (in the LocalBroadcastManager case) must be still active after onStop(), so I would register in onCreate() and unregister in onDestroy().
Use below code in your exit button
Intent intent = new Intent(this, ActivityOne.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
And, in your ActivityOne.class oncreate() method just put below code
if (getIntent().getBooleanExtra("EXIT", false))
{
finish();
}
As a few answers say, call finish(); after you start an intent from an activity. That will make sure the current activity is the only running activity. Also try super.finish(), which will call finish() at parent activity.
You can alternatively use only one activity with many fragments. That way, if you call finish() in the exit button OnClickListener code, you'll exit the app. This will also save a lot of coding since you will be defining the navigation drawer once.
First of all, Android apps are not intended to be killed other that by the system, which attempts to keep them in memory in case the user will return.
But, if you have to do it, try this:
android.os.Process.killProcess(android.os.Process.myPid());
hi I want to close my application and also want kill it from ram by clicking button.
i am using this method, but it just minimize application and didn't
close complete app. what to do ?
public void AppExit()
{
this.finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
Calling:
System.exit(0);
Process.killProcess(android.os.Process.myPid());
Both of which are definitely not a good idea!
Instead use the activity's finish(); method - let Android handle the cleanup of RAM management, which it is very good in doing so!
If you do not want your activity to be shown in the activity stack, the flags,
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP would usually suffice for launching another activity.
OR specify in your AndroidManifest.xml in your activity tag, android:noHistory="true".
System.exit will exit the VM and Android will kill this process.
Activity#finish() is usually the popular choice, but this does not need to be executed right away and also only affects this particular Activity, not the whole process.
Just FYI: killing apps manually is not a good app design in Android (except maybe some special situations). If you could explain why you want to kill your app, we can probably tell you a better solution.
use single line code
System.exit(1);
or
android.os.Process.killProcess(android.os.Process.myPid());
I am looking for a way to launch another app from within my app but so that the focus is not changed from my app to the app launched.
I.e currently I have the new app launched via a intent, however when this is carried out the new app is launched and becomes the app in view, I need it to be kept in the background with my app still in view.
The reason for this?
I am developing an application for internal use that will act like a lock-screen to the device so although things must happen in the background the 'lock-screen' must always be on top.
I have done some research into intents and launching other apps but can not find anything about what I need.
Hope you can help thank you!
Currently the terminal is called like this:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("jackpal.androidterm", "jackpal.androidterm.RemoteInterface"));
intent.setAction("jackpal.androidterm.RUN_SCRIPT");
intent.putExtra("jackpal.androidterm.iInitialCommand", cmdString);
The reason it needs to be running in the background is so that the app can run commands in terminal without the user having access, but then they 'unlock' the screen they need to then be able to view the terminal and what commands are being run etc
You can not startActivity in Background.Instead start the activity and minimise the activity(in your case this activity is of different application) using moveTaskToBack(true);
In your case, put a condition based on your intent and its params and use moveTaskToBack(true); so that activity will be minimised only when your application launches.
This won't be possible, you will have to start a background Service that does the actual work and only launch the Activity you want to navigate to once your foreground Activity is finished. Depending on your architecture, you can store the Activity to call when your foreground Activity is finished and change it from the service. That way you will have your desire behaviour without having to actually call the Activity.
In addition to the answer from #Meher, in the intent for your current starting activity, you can add the flag FLAG_FROM_BACKGROUND.
With this you get rid of the "blinking" effect (the one where your activity shows for one fraction of second while it discovers wether to go to background)
I am implementing oauth login in Android, and I'm stuck on one point. I am required to redirect to the native browser to initiate the login (rather than use an embedded WebView of my own), but that is causing issues with the back stack. My goal is simple: return to my activity after the redirect with no trace of the native browser in the back stack. Here is the closest I've come so far:
To initiate the login from my activity I use:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
context.startActivity(intent);
I have a separate activity to receive the return redirect and handle the rest of the login process. I don't really care whether it's a separate activity or not, it is just my current solution. In its manifest I set android:noHistory="true". Once it finishes the login process, I attempt to return to the first activity using code like this:
Intent intent = new Intent(context, OrigActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
However, this ends up creating a second copy of the original activity, and when I press back it returns me to the first copy. In other words, it does not seem to respect FLAG_ACTIVITY_CLEAR_TOP. Did I miss something simple to make it work, despite all my searching? Or is there some other way I should go about all of this?
Edit: It turns out the problem stems from the fact that the browser is launched in a separate task. From some Android documentation:
... if your application issues an intent to open the Android Browser, its activity is not placed in the same task as your application. Instead, either a new task starts for the Browser or, if the Browser already has a task running in the background, that task is brought forward to handle the new intent.
If I set my original activity's launch mode to singleTask it properly brings it to the foreground instead of creating a new copy, but a new problem occurs. The browser is now in the back stack behind all of my activities, so now it seems FLAG_ACTIVITY_NO_HISTORY is not being respected. I'm not sure if that brings me any closer to a proper solution ...
Edit 2: Correction: if the browser was not running before I launched it, everything works perfectly. However, if it WAS already running FLAG_ACTIVITY_NO_HISTORY seems to have no effect on it.
What you're seeing makes sense because calling the browser with the FLAG_ACTIVITY_NO_HISTORY wouldn't remove back stack from PRIOR to the start of your application.
i have a button to close my app with this code:
finish();
the problem is that this button doesn't exit of my app... it simply closes the current intent ant returns to the previous intent (window) of my app....
how i can do a real exit/close button?
i tryed with this:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but it doesn't works, because when i turn back into my app, the app comes in the last opened window, and not in the first window of the app, how i can do that? i need that when i re-open my app it starts on the first window of my app
If you really want your app to die. You could initiate each intent with startActivityForResult(). then before each finish() set the result to send back. in each parent activity you can override onActivityResult() to test whether the result received means the application needs to end. if so you can call another set result and finish(). repeat this in all activities and you will find that your application terminates entirely.
Incidentally I'm writing this from memory. function names may not be exact.
Hope that helps.
p.s. re-read your requirements. you can always stop the finish loop at your first activity.
I would do it this way:
I would define my initial activity (i.e. MainMenu) with a Launch Mode of singleTop
I would then invoke my MainMenu from the activity that is going to close the application.
startActivity(new Intent(this, MainMenu.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("closeProgram", true);
Then override the onNewIntent in the MainMenu activity; check for the extra boolean of "closeProgram", if the value is true, then do a finish();
Haven't tried it but I think it should work.
I recommend you read this: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html
Chances are, you don't want an exit button. Perhaps a logout button, but that's it.
finish() closes activity (this is what you call intent, but it's not correct), not application. Application can not be finished at all (only forcefully killed like task killers do). You should design your activity stack in such a way that it will suit your needs. May be you should look at stack rearrangement examples in ApiDemos.
you can try this: System.exit(0);