i know that this question is asked many times, but really i can't understand the answer, i want to set button, when user click it i want to exit the application (also the carbage collector should remove the objects), and after exiting i want to go to the screen where the user found the application icon on mobile.
for exit i don't know what to do
for going to the screen where to find the application icon i tried like this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but doesn't work
use this method, this will make your application to go in backgroud and will open home/app_menu screen
moveTaskToBack(true); // method available in activity
reference link
When you start that activity, it will effectively stop your application's foreground activities. Android is designed to NOT shutdown the app so that it may be restarted faster.
However, if you really really want to purge the app from memory, then what you want to do is kill exit() on the Runtime singleton : http://developer.android.com/reference/java/lang/Runtime.html#exit(int)
Related
I wanted a way to exit my app. Hence I searched and found a piece of code which does that.
But I am not able to understand the code and why it does what it does.
Can anyone please explain?
Here is my code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
It's starting the action ACTION_MAIN that has the category CATEGORY_HOME. This corresponds to:
This is the home activity, that is the first activity that is displayed when the device boots.
So, this means the launcher. So, this is a fancy way of starting the launcher app.
Also, the flag FLAG_ACTIVITY_NEW_TASK means it will be started on a new task, not the same as your app is running.
Notice that this will not finish your app, it will just put it in the background (like switching to another app using the task switcher, just this app happens to be the launcher). The effect is the same as pressing the home button.
Also notice that if the user happens to have two launcher apps, and it has not selected the default one, it will get a chooser asking to select which one of the two (or more) apps wants to use. Not the best experience.
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);
I want to add "exit" functionality.
Display.getInstance().exitApplication(); almost works, but leaves
behind a "ghost" window in the android open applications view.
Why don't you finish all Activities and Services, and set every other reference to null.
That would leave you with just the Application Singleton alive if you have one.
Every Application on android OS, is like a User in Linux OS, and every application runs in a process which has a Process ID assigned to it which application does not know, but we if we kill the process and entire application will stop.
Here is how you can do.
android.os.Process.killProcess(android.os.Process.myPid());
but this would restart your first activity of your application, as android will try to recover the Application, you can avoid that by going to home screen like this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Display.exitAppliction is the only way to exit an app but it won't be removed from the task switcher. Thanks to the comment from #david-medenjak I was able to find this:
android:noHistory="true"
To use that just use:
android.xactivity=android:noHistory="true"
In your build hints and the app won't be in history at all. But as people said this is a pretty bad practice...
FYI in iOS if you include an exit button your app will be rejected by review unless its a "logoff" button. Typical mobile apps never really exit and are only supposed to exit for security purposes.
The OS will kill your app if it needs the resources. Notice that this is important since mobile apps are stopped/restarted often e.g. incoming phone call.
My application is running normally and when I move on some new activity I used the code as
Intent start=new Intent(current activity.this,new activity.class);
startActivityForResult(start, 0);
finish();
and then when I have to move back to the earlier activity I used the following code
Intent start = new Intent(current activity.this,earlier activity.class);
startActivity(start);
finishActivity(0);
I think in this way the stack of the activities may be clear. And on click of the button or the phone back button I used the following code
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startMain);
But my application is still running in the background. I also used the android:noHistory="true" app is killed from the background but message appered that app close forcefully. Simple finish(); moves to the last activity.
I also seen the link Quitting an application - is that frowned upon? but I am new so did't get much. Please suggest me some way how can I deal with this.
I think in this way the stack of the activities may be clear
You are welcome to your opinion. startActivityForResult() is not designed for how you are using it.
And on click of the button or the phone back button I used the following code
I would fire anyone who worked for me who did this.
Please follow the Android design guidelines for proper handling of the BACK button -- while I do not agree with everything in there, it is much better than what you are doing. In the eyes of the user, BACK means back.
But my application is still running in the background
So long as you have not leaked any threads or services, your process will be running, but it will be consuming no significant CPU time. Android will terminate the process when it needs the RAM. This way, if the user elects to return to your application, or something else in your application needs to run (e.g., AlarmManager event, broadcast Intent), Android does not have to waste CPU time and battery life to fork another process.
Please suggest me some way how can I deal with this.
Just leave the process alone, and nobody will get hurt.
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.