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
Related
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.
I have my launcher activity A which from there starts activity B (based on fb login). I finish activity A before starting B. I also tried using flags such as
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
However sometimes while working with activity B, all of the sudden Activity A pops up. I have no idea where to debug or where to see why A is being brought to the front all of the sudden? Any hints how this can be traced?
I wouldve shared code but sharing thousands of lines wouldn't make sense :)
EDIT:
I tried to simplify the question. Here is more details, in reality There are 3 activities.
A->B->C
A is the launcher activity which takes you to activity B after you choose LOGIN.
B you put the info, then takes you to C. C is where I want to stay on. Somehow, Activity A comes up after some time. If I hit the back button, it takes me back to activity C ( which what I want)
Activity A to B code:
intent = new Intent(LoginRegisterActivity.this, LoginActivity.class);
startActivity(intent);
Activity B to C code:
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Intent.FLAG_ACTIVITY_CLEAR_TASK apparently the flag is ignored for API lower than 11.You can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK and import import android.support.v4.content.IntentCompat; from support library for work on API 10.
I need to clear the activity stack. All the activity except the current one in SDK 8. I want after logout from any activity I can reach to the Login Activity and All previous Activity will get Removed from the activity stack.
I have tried this solution
Intent i = new Intent(BaseActivity.this, LoginActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
But it is not working. I can't use finish() because user can logout from any activity.
Your Help is Highly appreciable.
Just use this flag on its own Intent. FLAG_ACTIVITY_CLEAR_TASK
Use This
Normally when we launch new activity, it’s previous activities will be kept in a queue like a stack of activities. So if you want to kill all the previous activities, just follow these methods
Intent i = new Intent(OldActivity.this, NewActivity.class);
// set the new task and clear flags
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(i);
I have got two Beautiful solution of my problem.
SOLUTION 1:
One is by using Broadcast Receiver. That I can broadcast a message to all my activity and onRecive() I can finish all my activity. As this Link says.
SOLUTION 2 (A Much Easier way):
Second is by managing a flag in ShearedPrefrences or in Application Activity. In starting of app (on splash screen) set the flag = false; OnLogout click event, just set the flag true and in OnResume() of every activity check if flag is true then call finish().
It works like a charm :) And please If you can't answer at least don't abuse the question.
I need to clear the back history of activities.
I have 5 Activities.
In the first 4 activities I have to make the back button go to and fro with data filled in all the screens. But as I proceed on 5th screen I need to reset the back history and start intent with new stack. So that after i press back in my 5th screen app should exit. I tried using launchMode , Nohistory and Flags such as NEW_TASK , CLEAR_TOP, CLEAR_TASK but still unable to achieve this. Please help me. Suggest me how to achieve this.
In your comments you suggested that you've tried using flags like:
Intent i = new Intent(MainActivity.this,SecondActivity.Class);
i.addFlags(android.flags.FLAG_CLEAR_TOP | FLAG_NEW_TASK | FLAG_CLEAR_TASK)
But instead try using them like:
Intent intent = new Intent(MainActivity.this,SecondActivity.Class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
I have a multiple activities in my application. Two of them are a LoginActivity and the second one is a SettingsActivity. The user logs into the Settings activity and logs out of the application from the SettingsActivity. The flow of the application is LoginActivity -> HomeActivity -> SettingsActivity . The user calls the logout from the application in the Settings Activity. I call finish on the Settings activity and create an intent to the LoginActivity. This works fine, but when i press the back on the loginactivity the home activity appears. I want that once the LoginActivity appears the back button should take the application out of the application. How can i do this ?
The answer lies in FLAG_ACTIVITY_CLEAR_TOP.
All you need to do is set the flag to your intent like shown below
Intent i = new Intent(this, LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
That should pretty much do what you want it to do.
I believe this is what you are looking for:
How to clear the Android Stack of activities?
I wouldnt get too hung up on the fact that it doesnt actually finish() your LoginActivity - this is actually a pretty normal pattern to follow.
I had to use .FLAG_ACTIVITY_CLEAR_TASK for this to work . I read this is supported after API version 11, will these do for all previous versions. Secondly how can i use these from the Manifest file and not the java code ?
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);