My App has an Activity A which triggers B and B in turn triggers C.
Activity A is the launcher with the intent flag android:noHistory="true" in manifest file. This shows the splash screen. After 2 seconds it triggers B with the intent flag FLAG_ACTIVITY_NO_HISTORY. B triggers C normally without any intent flag. Now the Stack will have only the Activity C.
In Activity C, Whenever back is pressed It will trigger Activity B. Now the Stack should have the Activity B alone. It should not have any trace of other Activity.
I have used the following code in the Activity C. But the flag FLAG_ACTIVITY_NEW_TASK is available only from API 11. The app should be supporting the devices from the API 8. Kindly assist me with the correct intent flags to achieve the explained scenario
Code:
Intent dragDropIntent = new Intent("android.intent.action.DRAGDROP");
dragDropIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
dragDropIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
I agree that starting the Activity C without nohistory will help. But do t some technical constraints I don't want to do that. Thanks in Advance
you are looking for this:
Intent intent = new Intent(activity, activityClass);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
activity.startActivity(mainIntent);
use android compatibillity lib from google - found in the sdk.
or use finish()
This flag is now available through the compatibility lib:
IntentCompat.FLAG_ACTIVITY_CLEAR_TASK
Hope this helps :)
If C is always goes back to B, then you should not use FLAG_ACTIVITY_NO_HISTORY. Then when the user backpress in C, C would be finished by the OS and the stack now only has B.
Related
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.
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 have got 4 activity let it be A->B->C->D.In every A,B,C activity user need to enter data all data will sent to server in C activity if the user data is correct he will move on to D activity and all the activity A,B,C removed from stack.If the data is in correct i need give the user to reenter data i.e is on back press it has to move C->B->A.My question is How to remove A,B,C activity when user enter D activity.
Use FLAG_ACTIVITY_CLEAR_TOP this shall solve your problem
From the Android documentation:
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
For example, 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 it like
Intent intent = new Intent(getApplicationContext(),
yourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
and also, take a look at this question:
Activity with Flag FLAG_ACTIVITY_CLEAR_TOP (android)
Edit : I thought you want to move to your home activity from D and want to remove all activities from stack
Your stack would be like homeactivity , A , B , C , D
so i gave you this solution as this shall remove all activities in stack on top of your home activity.
If you want to clear the the stack while going to D, for that you can use FLAG_ACTIVITY_TASK_ON_HOME or FLAG_ACTIVITY_CLEAR_TASK
But both of these for api level 11.
The correct answer interesting for me also, but I can offer a solution:
For example you start activity A from O: O->A->B->C->D.
On activity O you can put in android manifest android:launchMode="singleTop"
Then, when data are ok, you can start activity O with flag "FLAG_ACTIVITY_CLEAR_TOP" - it remove from stack A,B,C and will be called method onNewIntent (Intent intent) http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent) in O, where you can start activity D.
You can start activities with startActivityForResult, and call setResult from for example activity D, in C activity you can listen activity result, and related of this result finish activity or not, or call setResalt from C activity ...
When you want to move D activity there you need to check your
conditions and if your condition is satisfied then you need to enter
into your Next activity(i.e., D) .In that case you need to use the
following code..
Intent intent = new Intent(this,D.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Suppose In on backpress you need to use finish(). to move back i.e., C
-> B -> A.
Try this piece of code with some modifications:
Intent intent = new Intent(this, D.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // To clean up all activities
startActivity(intent);
Try looking for Activity's public method startActivity(Intent i) and finish() here
In usage wise, it should look like this.
Intent i = new Intent(MainActivity.this, MainActivity.class);
startActivity(i);
finish();
Hope this helps :D
I have an application in which i want to navigate this way:
A -> B -> C
And from C launch a new activity called D, that goes to top and also clears the task, meaning that if i press back on D it goes to home screen.
If i understand it correctly this FLAG: FLAG_ACTIVITY_TASK_ON_HOME does this... but it's only on current APIs (11>).
I'm developing for Android 1.5> how can i also have this behaviour?
Thanks!
you are looking for this:
Intent intent = new Intent(activity, activityClass);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
activity.startActivity(mainIntent);
use android compatibillity lib from google - found in the sdk.
or use finish()
You can use FLAG_ACTIVITY_CLEAR_TASK along with FLAG_ACTIVITY_NEW_TASK.
public static final int FLAG_ACTIVITY_CLEAR_TASK
Since: API Level 11
If set in an Intent passed to Context.startActivity(), this flag will
cause any existing task that would be associated with the activity to
be cleared before the activity is started. That is, the activity
becomes the new root of an otherwise empty task, and any old
activities are finished. This can only be used in conjunction with
FLAG_ACTIVITY_NEW_TASK.
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK
I've ended up using this https://stackoverflow.com/a/8576529/327011 as I needed to use it from 1.5 > and that flag is only avaliable on higher APIs.
If I hadn't the need to support "low" API levels the answer from #aromero would be my option.