Hello i am new to android.I am implementing some application and it have some activities.
Suppose if i launch the app for first time,it's entering in to A then going to B after that C,D,E..... (Here A,B,C,D,E are activities).If i press back button at E then it is going D--> C--> B--> A like this.
Now i want to implement code to exit/quit from the app when i am at D.
I wrote following code but this code is working for closing current activity and going to prev activity.means going C.
finish();
Then i tried with following code and it is working fine and closing current application successfully and going to device home screen.But if i want open the application again then it is starting form D instead of A.
intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
copied from here
Please help me to solve my problem.
Use these flags to lunch the activity and clear the activity stack
Intent intent = new Intent(this, YourActivityD.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Be aware that FLAG_ACTIVITY_CLEAR_TASK is only available from API 11
See: http://developer.android.com/guide/components/tasks-and-back-stack.html
Try this:
finish();
System.exit(0);
Related
I know this question has been answered in another posts also. But it doesn't solve my issue.
I have 3 screens: A,B and Home. App starts with A, then goes to B and then to Home page. But I want to delete activities A and B when it reaches Home activity, so that when back is pressed app exits.
I tried:
Intent.FLAG_ACTIVITY_CLEAR_TOP. It works fine when back is pressed. App quits. But if I again open the app from background, it starts with screen B.
Intent.FLAG_ACTIVITY_NEW_TASK. This works exactly as I want. But when Home activity starts, there is a sudden glitch in the screen and it is not smooth.
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will clear all the activities on top of home.
Try the following code:
Intent i = new Intent(YourActivity.this, HomeActivity.class);
i2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(i);
You can do this way also, for your splashScreen(A) you can set nohistory=true in AndroidMenifest.xml, and goto Activity(B), and when you are going on Activity Home from Activity(B) finish activity(B) than goto Home activity.
so both of your previous activities will not store in stack.
And on Activity B override onBackPresh() method but remove super.backpresh() in side it. and start your activity A with finish this current activity, it will work.
I'm making an app that shows another activity when I correctly login however in the home screen when I pressed back and then returned to the app the login activity showed.
I was able to avoid this overriding the onBackPressed this way:
public void onBackPressed()
moveTaskToBack(true);
}
Is this the best way to do it? Is there a more proper way to keep the state of the application when I exit it?
In your login activity after starting intent call finish()
finish destroy your activity and avoid to run it again automatically.
Your issue is that you kept LoginActivity in stack. so when you press back it will kill MainActivity(after login) and since LoginAcitivity still there. it will be shown again. best is to kill LoginAcitivty after starting new activity.
call this in LoginActivity:
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
finish();
As you question I understand, you want to clear the entire history stack and start a new activity. For this, in API level 11 a new Intent Flag was added which is: Intent.FLAG_ACTIVITY_CLEAR_TASK
Just to clarify, use this:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
For API <=10 now one can use IntentCompat class for the same. One can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK flag to clear task. So you can support pre API level 11 as well.
To lay it out as simply as possible. My user enters the app through Activity A. They "login" and then Activity B is started, and Activity A is finished using finish() so that the users can't get back to the login screen by pressing back from Activity B. Now in Activity B, they move to Activity C, and in activity C, I want them to start a new intent that will start activity A, but "kill"/"finish" all other activities. The only way I can think of doing this is using an Intent Flag. so far I've come up with:
Intent intent = new Intent(getActivity(), ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);
But the code above doesn't work. I still see that my app had a savedInstanceState in Activity B. Hence something like CLEAR_TASK won't work for me case.
EDIT: Also, is there an easy way to tell have many "tasks" your app has opened currently?
Note: There seems to be other flags that may help, but they were added in API 11, and I need to support API 10.
Flag combos I've tried:
Doesn't work
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Doesn't work
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
You need to use IntentCompat. like
import android.support.v4.content.IntentCompat;
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
Check whether the below answer helps you:
http://www.hrupin.com/2011/10/how-to-finish-all-activities-in-your-android-application-through-simple-call
I have an android activity problem.
Here is how my process works:
Login Activity starts
Login successful. MainMenuActivity starts and LoginActivity is finished by me.
User touched on settings and SettingsActivity starts. MainMenuActivity is NOT finished. because is it the main menu. when user presses the back on settings screen I need to go back MainMenuActivity. so I cant kill MainMenu.
User touched on log out and SettingsActivity is finished by me and Login activity starts. As user returns the login I need to kill MainMenuActivity but I cant.:/
I tried FLAG_ACTIVITY_SINGLE_TOP, CLEAR_TOP, SINGLE_TASK, NEW_TASK, NO_HISTORY etc.. almost all of them didnt work
I put launchMode="singleTask", clearTaskOnLaunh="true" etc. didtn work again.
I tried addFlags() and setFlags() both, didnt work
There are some many issues about this topic here, I read and applied all the suggested solutions and didnt work.
Can anyone help, please?
P.S android:minSdkVersion="8" and android:targetSdkVersion="15" for my app. I didnt use fragments in the app, I use old activity structure.
Use the combination of two flags like this:
Intent intent = new Intent(this, Login.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
This deletes all the other activities and starts this one.
Try this.
For api level <11
i.addFlag(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
I'd suggest to start the settings activity for result (see here), and when the user requests to logout, set a result accordingly. You will get this result in MainActivity's onActivityResult (see here) and can handle the logout there, finishing the mainActivity before starting the loginActivity.
I suggest that you don't finish the Login activity when you start the main menu. Then you can always clear all activities on logout by doing this:
Intent intent = new Intent(this, Login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will only work if Login activity is still active at the root (beginning) of the task stack.
To prevent the user from BACKing into the Login activity from the Main activity, you can override onBackPressed() in Main activity and do something else.
Use FLAG_ACTIVITY_CLEAR_TOP like this-
Intent loginIntent = new Intent(this, Login.class);
loginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
finish();
Intent intent = new Intent(MainActivity.this, MyActivity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
in this example Mainactivity automatically closed and Myactivity2 Start.
and one thing you need to add finish();
When I press the log out button and come to start(main) screen and when I press the back button from main screen it comes to 2nd screen. but, i want to close the application on click of back button from main screen and close all the activities before that.
while doing research I found,
Intent intent = new Intent(this, Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
but its not satisfying my requirement.
You can use this code that I referred from here How to exit from the application and show the home screen?:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Although Android's design does not favor exiting an application by choice.
Related Links:
How to close Android application?
Android exit application
just add line in your code and this will work fine
startActivity(intent);