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.
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.
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
Lets say I have
A->B->C->D->E
In android back stack. I want to be able to get back to one of the following:
A->B->C
A->B
A
How can I achieve this? Hopefully without forcing back button clicks.
Using the image and information from the official developers page on Android tasks and back stack you can see that of all other ways to launch an Activity you can ensure such behavior only using the FLAG_ACTIVITY_CLEAR_TOP in your Intent flags.
Your regular back button proceeds as:
But when you specify this flag, you get a behavior like you need, as given by an example at this source:
consider a task consisting of the activities: A, B, C, D. If D calls
startActivity() with an Intent that resolves to the component of
activity B, then C and D will be finished and B receive the given
Intent, resulting in the stack now being: A, B.
Use FLAG_ACTIVITY_CLEAR_TOP flag.
Intent a = new Intent(this, A.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);
Actually , to go "up" to the activity of your choice, you should use the "up" navigation as used on the action bar:
/** used to handle the "up" button on the action bar, to go to the defined top activity as written on the manifest */
public static void goUpToTopActivity(final Activity currentActivity) {
final Intent intent = NavUtils.getParentActivityIntent(currentActivity);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
NavUtils.navigateUpTo(currentActivity, intent);
}
in order to use it, you must set on the manifest to which activity this function should use (or you could of course set it yourself by changing the code) :
if you use actionBarSherlock, for each activity that you wish to let to go up, use:
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.your_app.activities.MainActivity" />
if you use the android framework (if your minSdk version is API 16 and above), use the "parentActivityName" attribute.
Suppose you are using Intent to move to another activity
Intent i = new Intent(A.this,B.class);
startActivity(i);
this code will take you to the 'B' Activity and when you press Back button it will again take you to the 'A' Activity . If you dont want to go back to activity 'A' you can use....
Intent i = new Intent(A.this,B.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
For more information about Back Stack in Android Follow this link :
http://developer.android.com/guide/components/tasks-and-back-stack.html
I need your help, I have 2 activities A and B where A is the main activity.
Now B starts the activities from A using startActivityForResult() and from B when I finish it will go back to activity A.
This works fine but the actual purpose was like the Gmail application when you go back from B to A and then start activity B again from A, then A need to start activity with its last screen as i left it.
For example: from inbox->label->draft in gmail how to achieve this to keep data/layout as it is.
Maybe android:launchMode="singleInstance" on activity A and B will get it done?
How to switch Activity from 2 activities without losing its data and current state of data
Ok I have got solution that when every time start your application that time only activity will be created and view and put in stack so the next time you start again this activity just open from background so the activity will available in stack and just brought to front using start activity
From A to start ActivityB
Intent intent = new Intent(context,ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Now for start ActivityA from ActivityB
Intent intent = new Intent(getInstance(), ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
It just brought front while activity start and put the current activity into background
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);