How to Clear all previous activities~? - android

Activity Flow in my app.
WelcomeActivity -> SignInActivity -> SignUpActivity ->
TabsActivity(this is main) -> ...
I want to close all previous activities (Welcome, SignIn, SignUp) when start TabsActivity.
I try several method...
TabsActivity. clear task on launch=true ? but not work (maybe)
TabsActivity. launch mode = singleTask ? but not work
But I do not want to "save 3 activities and call each activity.finish()"
Depending on the situation, "available 2 or 4 activities not 3", or
"I do not know What activities is in activity stack".
I want to clear all previous activities, regadless of any situation.
Help me :)
Sorry my poor english... Thanks.

If I understand correctly, you might want to try starting your TabsActivity with the following code:
Intent intent = new Intent(getApplicationContext(), TabsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
The flag Intent.FLAG_ACTIVITY_CLEAR_TOP clears the history.

Use
Intent intent = new Intent(getApplicationContext(), ClassToLaunch.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will finish the previous activities

To Close Previous Activities ,you should start New Activity with startActivityForResult and then before finishing the current Activity with finish() call , setResult(value) for previous Activity ,the previous Activity will then get a callback where you can call finish() for the previous Activity.

Clear Backstack of android, from where you are calling tabActivity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Related

When do I use addFlags or setFlags for the purpose of removing activities in stack?

I have OnBoard Activity and Login Activity. In LoginActivity after successful login I am trying to clear onboard activity using below code:
startActivity(new Intent(context, HomeActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK));
I've even tried Intent.FLAG_ACTIVITY_NO_HISTORY but it's also not working. So what should I do?
I am assuming that the app starts with OnboardActivity and that starts LoginActivity and then you want to clear them both and launch HomeActivity?
If that is the case, I would do it this way:
OnboardActivity launches LoginActivity using startActivityForResult().
LoginActivity returns a result that indicates if the login was successful or not and calls finish(). LoginActivity is no longer in the task.
OnboardActivity checks the result in OnActivityResult() and, if the login was successful, launches HomeActivity (no flags needed) and calls finish() on itself.
At this point, both LoginActivity and OnboardActivity are gone, and HomeActivity is the only Activity in the task.
Try below code:
Intent intent = new Intent(context, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
I would say go with the finishAffinity() before moving to onboard activity. There is no need to set any flags. Because this method kills all activities in the stack and current activity as well
There is no need to use FLAGS. Even though you want to understand, Check this
There are two ways to finish Current Activity (LoginActivity in your case) and move to Next Activity (OnBoard in your case).:
Call finish() before starting Second Activity. It will destroy current activity.
Call finishAffinity(); before starting Second Activity. It will destroy stack of all previous activity.
Hope it will helps you.

How could I close all opened activities other than MainActivity

When my activity stack is like,
MainActivtiy -> Activity1 -> Activity2 -> Activity3
I need to go 3 back states to reach MainActivtiy from Activity3
I could be able to close opened activities from Activity3 like,
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
But, above code segment closes all activities including MainActivtiy and starts new MainActivtiy. I want to know are there any other options to do the task that I need. I dont want to create new Intent because it drops the data of static methods that I have created in my MainActivtiy.
Please help me, Thanks in advance.
You should use FLAG_ACTIVITY_CLEAR_TOP which will bring the running activity on top and remove all other activities above it
Note : it will trigger the onNewIntent of already running activity otherwise mention launchMode = "singleTask" in activity tag, inside manifest

Android:Delete a activity in backstack

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

Go back to previous screen without creating new instance

As explained in image, flow is something like this. So whenever user click on logo button Activity A should be called. As simple solution we can use this method...
Intent intent = new Intent(activity, activityToStart);
startActivity(intent);
But this activity will create a new activity for my app. but I need to call the same instance of the activity as we move forward in flow diagram. from Activity A to B and then again on B can be called easily by callingfinish() but from Activity C or D, how to come back to A.
I am running out of ideas but not getting any fruitful result. Please help me if you have any suggestion or at any place i am going wrong. Thanks in advance.
To Come Back from D to A, use Intent Flags.
Intent intent = new Intent(activity, activityToStart);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent);
FLAG_ACTIVITY_CLEAR_TOP, will instead of creating new activity, it will invoke the activity on the stack, and will pop all the activities over the activity being invoked.
Instead of Using
Intent intent = new Intent(activity, activityToStart);
startActivity(intent);
Use
Intent intent = new Intent(activity, activityToStart);
startActivityforResult(intent,1234);
This will make Sure that The Activity A is not Killed and when You finish Your Activity C,Activity A will get Resumed.
Note :- Whenever You create A new Activity,without finishing(Exiting) the Host Activity,The Host Activity is Saved On the Stack in LIFO order
LIFO:- Last In First Out
By making Activity A a "SingleTask" you can achive this. When a Activity is in the SingleTask on clicking the Home button the other activites will be removed from the stack.
Refer these link's for more info on Android Activites...
Link 1 - Android Fundamentals
Link 2 - Another Similar Question
Use ViewFlipper to go and back between different window in the same activity.
ViewFlipper vf = (ViewFlipper) findViewById( R.id.view_flipper);
To go to the next window
vf.showNext();
To go to the previous window
vf.showPrevious();
I am not sure,this is right way or not,but you can give it a try!
You can finish() current activity when you open the new one starting from Activity-B.
i.e.
To open Activity-C => finish Activity-B and start Activity-C
To open Activity-D => finish Activity-C and start Activity-D
now when you will press back,Activity-A will open.

How to finish all child activities from parent in android [duplicate]

I'm new to android. Actually one handler is running in Home Activity A for every 30 sec to check the net connection.
If I'm went to activity C by A->B->C, If there in no net connection at that time, then i want to close Activity B and C, then want to show message box in Activity A.
But My problem is My handler is running for every 30 sec in Home Activity A. But If i was in Activity C or some other Activity how to find which activity is my Application currently focussed now. Then i want to finish those child activities and want to show Home Activity A I have some 9 child activities in Activity B.
I heard about using "FLAG_ACTIVITY_CLEAR_TOP" . I used the code as follows in the handler in Home activity A itself. But got error.
Intent intent = new Intent( ctx, Homepage.class );
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP);
ctx.startActivity( intent );
Here Homepage.class is my Home Activity A and i set that activity in manifest file as
android:launchMode="singleTop"
Please help!
You can start Activity A and close all other activities.
You have to create new intent and add flag FLAG_ACTIVITY_CLEAR_TOP
Intent activityA = new Intent(context, ActivityA.class);
activityA.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.StartActivity(activityA);
this will close all activities that are in the stack and are at the top of activity A
When calling Acitivity C, call finish in activity B , when calling Activity A from C , call finish() in activity C !
You can use, According to me two ways,
If you start an activity using startActivityForResult, then you can call finish() in this new Activity when you're done with it and it will return control to the activity that started it.
OR otherwise, May be I am wrong,
Call finish inside onStop 'override method'.
make a uniform resultCode for closing child activities. Eg. you make 911 (should be int) as your resultCode. If you want your Activity to finish and go back directly to parent Activity, you set the resultCode to 911:
setResult(911); finish();
In every child activity, you override the onActivityResult and check if the resultCode is 911. If yes, then call the setResult(911); finish(); until you get back to your parent activity. Hope this helps!
For example, if you want to start intentB, you can do following in old activity:
Intent intentB = new Intent();
intentB.setClass(XYZ.this, abc.class);
intentB.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentB);

Categories

Resources