I want to close the application gracefully. I found two methods.
1. Using Intents:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Documentation says:
Activity Action: Start as a main entry point, does not expect to receive data.
Category Home: This is the home activity, that is the first activity that is displayed when the device boots.
2. Using Finish:
finish()
Documentation says:
Call this when your activity is done and should be closed.
What is the best method or professional method for closing the Android application? Both close the application but finish() removes the app from cache (App not in recent activities) while using intent, cache does not deleted. Should cache remove from the cell on finishing the activity?
The best way is the proper one, as it's said in the official doc :
Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().
The App is a succession of activities. Closing the app means closing the first activity of the app.
The first method you described in your Post (1. Using Intents) seems to be a workaround; it's similar to when the physical home button of your device is pressed. It lets the activity to the back stack and launch the Home (First) activity of the device.
Related
I have a login activity. After login, the Main activity is started, which uses fragments for user navigation. I want a button in the nav drawer of my main activity which will completely close the app
Now, I have seen many many threads on this and have tried implementing their solutions. For example:
I have tried finishAffinity() in my Main activity, which should close the current activity as well as all parent activities
I have tried using finish() on the Login activity as soon as I bring up the Main Activity, and then calling finish() again when the user clicks the button
The highest voted answer for this question: Close application and remove from recent apps/, also does not seem work. First, android:autoRemoveFromRecents="true" requires API > 21, but even if I set the minimum SDK version to 21, the app still remains in the list
Finally, I have tried using an Intent when the user clicks the quit button and navigating back to the Login activity, and setting flags with an exit extra, and then finishing the Login activity (i.e. exit android application programmatically)
None of these are working. They will all close the Main Activity, and maybe even close the Login activity. But if the user clicks the app list/current apps/open apps key (the square soft key on most phones), the app is still visible there. When the app is clicked in that list it will take me back to the Login activity screen (I'm unsure if this is starting the app from fresh, or whether its just taking me to the previous Login screen which didn't close)
Out of desperation I have even tried System.exit(0), which I know is bad, but even that doesn't remove the app from the app list
So, how do I programmatically completely quit an app and remove all traces of it being open?
EDIT: I was too hasty in claiming one of the answers below didnt work (see italics above). The answer does remove the app correctly
I think this is the solution you're looking for.
You might consider having another activity named ExitActivity which will be called when you try to exit from your application. The trick here is, the ExitActivity will have android:autoRemoveFromRecents set to true in the manifest file, so that your instance will be cleared automatically from the recents.
Based on my research:
There is no way to force quite an application in android. You can only pause an activity. The discretion to quit an app lies totally with android framework which it does on checking the memory and resource utilization of apps. I could not find the official link for now, but I had read it earlier.
manage it by the OS. bring up the home screen.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Clearing an activity stack via Intent is well documented with questions like this.
However this solution assumes that you are moving from one activity to another. Is there a way to clear this stack programmatically without having to start a new activity using an Intent?
To put this question in context:
My app has a database that synchronizes with a master online database. However the app needs to be available offline, so it has a completely replicated database stored locally in SQLite. I have an activity called FullSyncActivity which basically has a button called Start Sync. When the button is pressed, the sync starts (clears existing data and re-downloads from the server) and once the sync is complete, a new activity starts again.
The problem I have is that the user might navigate to the FullSyncActivity without fully understanding it's purpose - hence why I do not want to start the sync process when the activity opens (a Start Sync button is required). However, I cannot clear the activity stack when the activity is started (using an intent), because if the user cancels without pressing the Start Sync button, then they will have nowhere to return to. Ideally I'd like to clear the activity stack once the user presses the button. Is this possible?
I would do this by using a broadcast Intent. Have each activity register a receiver for a specific broadcast Intent. When the user clicks the Start Sync button, it should broadcast this Intent. When the receiver gets the Intent, it just calls finish(). In this way you can ensure that all activities end themselves when you send the broadcast Intent.
When would you need an Intent that takes you to the home screen? Because doesn't that mean you no longer have control of the application?
For example, what could you, as a developer, do after the following code was executed:
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
That is a much trickier question that you probably think. First of all, in real life you would not normally do that. If you are finished your activity, you call its finish() method. The android system returns you to the activity you called your activity from, which if you launched it from the home screen would be your homescreen. But if you launched it from some other application, like looking at a map of the address of one of your contacts for example, you would be returned to your contact app when you "finish()" on the map activity.
When you launch an activity with an intent, depending on the exact nature of that activity, you may open a new instance of that activity in the process your activity is running in, or you might just bring to the front a different process already running that application/activity. In the case of the home screen, I don't know exactly what happens because I don't know how the home screen is programmed as an application/activity, and how it is declared in the manifest.
For giggles, I put your lines of code in the onCreate() of the main activity of one of my applications. I got fairly erratic behavior. The intent definitely threw me out of my app and seems to have destroyed the process my application was running in in the process. (At least in eclipse, it terminated the ADB connection so I could no longer see what was happening with it.) When I went back to my main activity from the homescreen, it would sometimes go back to the main screen of my app, sometimes to the secondary screen of my app, and sometimes just pop back to the homescreen again. I imagine the other lines of code calling intents for my secondary app were part of the "state" of my app somehow, that somehow going back it would somehow come in after launching the home activity.
Obviously I am waffling around here. I'll leave this to others who might put an answer in the context of something that would really benefit from calling an intent to launch a homescreen, rather than using "finish()" to get away from the activity.
Can anyone tell me what the difference is between opening an app from the applications screen and opening it from that recently used apps list that pops up when you long-press the home button?
I didn't even know that recently used list existed until a friend managed to break my app by launching it from there. He tried twice and got the same force quit, but when he launched it from the applications screen it opened fine.
The error log told me that a nullPointerException occurred in the getCount method on my ArrayAdaptor for my ListView.
Anyway I just wondered if there was a difference that I need to know about and adapt my code to deal with?
AFAIK, If your application is completely shutted down, launch from applications screen and recently used apps list should have no difference, both refresh start your application and open your application's MainActivity (by stack-push your application's MainActivity into a newly created task)
However, as Android is multi-task OS, your application can be put into background in standby mode i.e. open your application then short-press home button, this is not same as press back button. If you haven't override these key pressed in your application, press back button several times with pop all your activities off from activity stack and finally kill your application, whereas press home button will bring System's HomeActivity into foreground hence flip your application (AKA. task with activity stack) into background.
Things becomes more interesting here, depend on which value your configure your activity's android:launchMode in AndroidManifest.xml, if you use standard or singleTop:
1. launch app from recently used apps list always bring your standby activity back to foreground, i.e. re-order activity stack.
2. launch app from applications screen will create a new instance of your MainActivity and open it, i.e. push a newly created MainActivity into activity stack, so now you have two instances in your application's activity stack
If you use singleTask or singleInstance:
2. launch app from applications screen will use the standby MainActivity (if exist) in your application's activity stack and re-open it, i.e. re-order activity stack.
Checkout Tasks and Back Stack to see how different configurations may affect your application's activity stack behaviour.
I believe there should be no difference. These are the lifecycle methods I typically see when pressing the home button from an activity, on android 2.3.4
onPause
onStop
then when I use either the icon or previous applications to navigate back, I see
onRestart
onStart
onResume
Now, in some cases the system will tell your activity to finish while you are away (or immediately when you return if an orientation change occurred). Then you will see onDestroy, and the following when you navigate back
onCreate
onStart
onResume
I don't think there is anything mysterious going on here. According to the Activity documentation, there are only four states that a process can be in, and both of these fall under background activity.
There shouldn't be any difference in how the activity is launched from history, apart from the fact that the launching Intent will have the FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY set.
Here's an easy way to think about it. All of your activities are launched form Intents. Holding down the home button allows you to open that activity using the last intent that launched it. This can give you some unexpected results however. For instance, if you are able to launch your activity from something special like a widget.
I want to disable the recent apps intent that is fired from the icon when you hold the home button. I am currently detecting, in the onCreate(), if the intent is fired from the quick launch dialog, and then I call finish().
This entails that when the application goes to the background finish() is called so that when the app is launched it goes through the onCreate(). This requires strategic coding when switching between activities in the application.
I guess I can fire finish() in the onResume() also after detecting where the intent came from, but a bunch of unnecessary coding can be eliminated if the quick launch intent can simply be disabled.
Can the quick launch intent be disabled?
Can the quick launch intent be
disabled?
Put android:excludeFromRecents="true" in the manifest for this activity. That will cause your activity not to appear in the list of recently used activities, which is what I am assuming you are calling the "quick launch".