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);
Related
I have an activity in my application that does a process and when finished, launches an activity with a normal intent:
Intent intent2 = new Intent();
intent2.setClass(anActivity.this, JustAnotherActivity.class);
startActivity(intent2);
finish();
The problem is that when you are doing the process, if I press the home button and take it to the background, when it finishes and calls the new activity, even if it is in the home of the phone, this new activity jumps to the foreground.
I am looking for the activity to be launched, but if the user is outside the application, it does not come to the foreground.
I guess the use of a FLAG is necessary, but the ones I have tried have not worked.
sorry for my bad english
Thanks.
what if you take care of ActivityLifecycle (OnPause, OnStop).
You can add a flag when the activity goes "OnPause/OnStop" depending what you want. And them perform the intent just if the activity is not "Pause/Stop".
Activity Lifecycle
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());
I am developing an application in android. I am new in android.
In my application I have a category selection activity from that user have to check a check-box, based on that he will get view on another screen. I have a menu button in 3rd screen in that I have a button for selecting category, when I click on that button its also works fine but when I click back button it will redirect me 2 times at same activity... How to remove this problem? I have used finish() method but its also creates problem its get's me out from the application directly...
I want redirect to selection activity and it should not show me 2 times when I click back button ....
Is there any way please redirect me thank you.
a call to finish() should work so you schould check your code for multiple calling the wrong method or something like this.
Intent intent = new Intent(activity, activityClass.class);
activity.startActivity(intent);
finish();
you should also take a look at the Intent flags:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Your question isn't clear enough for me to know if this will solve your problem, but if you include the following attributes on your Activity in AndroidManifest.xml, the so-attributed Activity will never appear in your history list.
android:excludeFromRecents="true"
android:noHistory="true"
As for removing something from the history, I'm not sure how to do that, but I'm interested in the answer!
You should call finish before you call Intent and go to next activity. so the current task is finished and will not be saved in stack and then you intent activity will come on top of stack. If you directly want to go to Selection activity, override onBackPressed() and intent to the activity you want to go to.
There are different flags that you can use to control how your activity interacts with the activity history stack. Two that might be of interest to you are FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NO_HISTORY.
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);
I wanna run two system activities one after another in specific order.
now as we know, startActivity is an asynchronous operation, so i cant keep on a specific order.
so i thought maybe I should try to do it with dialogBox in the middle but also running a dialogBox is an asynchronous.
now as i said the activities which i try to run are system activities, so i cant even start them with startActivityForResult (or mybe i can, but i cant think how it will help me).
Any tricks how could i manage with this issue?
Some code:
first activity:
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Settings.ACTION_APPLICATION_SETTINGS);
startActivity(intent);
second activity:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(tmpPackageFile
.getAbsoluteFile()),
"application/vnd.android.package-archive");
startActivity(intent);
as you can see, i dont have any access to those activites, i can just run thire intents from an outside class/activity/service.
You should be able to use startActivityForResult.. The second parameter to that function is a unique id, which you can use to track which activity is ending.
In onActivityResult of the calling activity, check which activity just finished, then start the next one with another call to startActivityForResult (or, if you don't care what happens with the 2nd, just startActivity).
I may be missing the boat on this, it seems you should place your code to start the second activity in the handler that finishes the first activity, such as on a button press or when an item is selected from a ListView. More information on how the first Activity is terminated would help.