Android finish current activity and start parent activity - android

I have a little question which is bothering me. How can I finish activity C and start it's parent. But the tricky part is that I can start activity C from 20 other activites. The idea is to open the right one when i call finish on C. And the other thing is that I have tabhost , which childs opens activity C.
Any suggestions how to achieve this?

in your activity C save the following variable:
Class parent = ParentActivityClass.class;
override:
public void onBackPressed(){
//create an intent like
Intent i = new Intent(this, parent);
startActivity(i);
//add extras to intent if needed
this.finish();
}
please note that this might create a NEW parent activity. it is up to you handle this situation if this might create problems.
An alternate solution is to finish each other child activity when you launch a new activity. This will assure that on your stack you will have always the parent below the child activity.

I simply did something like this :
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
This did the trick.

Depend on your activity stack if your current exactly on top of the parent activity you can just finish current actvity and it will go to previous activity. If you want to clear all activity stack and start over new activity try
Intent intent1 = new Intent(context, activity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
which clear your stack and start over new if you have stack of activity over parent want to finish all use this and start over parent again.

Take a look at Task and Backstack, Implementing Temporal and Ancestral Navigation
There are more specific explanations. Hope this help

Related

Go to fist activity when open many activity

In my app I have two fragment in ManiActivity.class .
In my fragment I have a listview , when I click a item in list I open MainActivity2.class . In MainActivity2.class I have listview related and when i click list related I open MainActivity2.class with new value. Now I want create a button back home in MainActivity2.class and when I open many MainAcitivty2.class when click list related on it I can click button back home to go to MainActivity2.class
I try it with
Intent intent = new Intent(MainActivity2.this,MainActivity.class)
startIntent(intent)
But when I click back button in MainActivity.class it comback MainActivity2.class , it not exit app.
How I do to clear it?
Please help me.
This is why Android maintains states of each activity in a Stack, that's why when you will press that Back-button, you just have to clear all previous activities from the stack and then open MainActivity
Just you have to set flags before starting intent:
Intent intent = new Intent(MainActivity2.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Use android:launchMode="singleTask" for your MainActivity in AndroidManifest.xml.
Which will keep only one instance of MainActivity if launched again this activity will come to top of the stack.
And Use this code
Intent intent = new Intent( MainActvity2.this, MyActivity.class );
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );
current_activity.startActivity( intent );
call finish method after startIntent
something like this:
Intent intent = new Intent(MainActivity2.this,MainActivity.class);
startIntent(intent);
finish();
and put finish method in your activity
#Override
public void finish() {
super.finish();
}
its very simple.. use finish();
Intent intent = new Intent(MainActivity2.this,MainActivity.class);
startIntent(intent);
finish();
bcz u create multi activity in mainActivity2. so u have to destroy your activity before u go to mainActivty.
otherwise when u open new many in mainactivity2. that time use finish.. bcz u must have to destroy first your mainActivity2.
if you not understand my answer then just post your whole code... i will edit...

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.

finish() activity twice in android?

Okay say your using a app, and you opened a new activity and then opened another, you can end the activity your on by using finish(); and your back one activity, but how can you go back two activities, all the way back to the first one? I know you could use:
Intent savedGameIntent = new Intent(v.getContext(), firstclass.class);
v.getContext().startActivity(savedGameIntent);
But is that the best way to do it?
Use the flag Intent.FLAG_ACTIVITY_CLEAR_TOP.
Intent intent = new Intent(this,A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
From the 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.
So effectively, if you have A -> B -> C, and you intent to A with that flag set, B and C will close.
I believe that will start a new activity, not back up to the original one. It sounds like you want to finish the last and middle activities. If you start the last activity with startActivityForResult, you can then override onActivityResult in the middle activity, and call finish() from there.

Android - start multiple activities

is it possible to start multiple activities at once? I mean, from main create 3 activities in some order and just the last will be visible? Up to now, I was able to create only one activity.
Thanks
You might need something like this in order to launch deep into the app after the user has clicked a notification in order to display some newly added content, for example.
Intent i = new Intent(this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
Intent j = new Intent(this, B.class);
j.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(j);
Intent k = new Intent(this, C.class);
startActivity(k);
In this way you can start activities A, B and C at the same time and suppress transitions to activities A and B. You get a single transition from your current activity to activity C. I strongly suggest that you log the Activity lifecycle method calls (onCreate etc.) to LogCat, for example. It helps a lot in understanding the order of events.
This can be a common thing to do in response to deep linking or other use cases where you, basically, need to synthetically rebuild the Task (and all the activities it should contain). Sometimes, just specifying parents in the manifest isn't enough.
Take a look at TaskStackBuilder. One common example:
TaskStackBuilder.create( context )
.addNextIntent( intentOnBottom )
// use this method if you want "intentOnTop" to have it's parent chain of activities added to the stack. Otherwise, more "addNextIntent" calls will do.
.addNextIntentWithParentStack( intentOnTop )
.startActivities();
Really old question but I thought I still answer it.
Use:
public void startActivities (Intent[] intents, Bundle options)
Try startActivity(new Intent(...); at the end of your onCreate-Method of the first Activity.
This will immediatly launch a new Activity and pause the first one.
With back-key you will get back to the last Activity

Categories

Resources