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...
Related
I'm working on an android application and in the application I have a couple buttons that let user to pass to another activity. Now the way I'm doing the transitions between this Intents is like below:
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
With the above content I start an activity and from that activity I'm getting back to the MainActivity using this code:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
But i suspect this cause memory to be over used because I'm not actually getting back to the Main Activity that created when application started, I'm just creating another instance of MainActivity I guess. Is this really as i thought and if it is how can I get back to the activity that created in the beginning or if I can't do such thing how can I make app to let the previous activity go?
Passing an intent to startActivity() will create a new instance of the activity and add it to the front of the stack. So:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
is basically asking to create a new instance. If you want to go back to the activity just before the current one, call either:
finish();
Or,
super.onBackPressed();
In your solution you just have to press back button and you'll be back in first activity.
If you want to close it and after open new instance like you are doing in second activity just add
finish();
at the end of
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
You just need to call finish(); method
Intent intent = new Intent(this, DestinationActivity.class);
startActivity(intent);
finish();
I have 3 activities in my application
ActA
ActB
ActC
Suppose I am in activity ActB and I am loading ActC with out finish(); ActB
Then when press a button in ActC , need to redirect the application to ActB . But this time when I press back from redirected ActB , another ActB ( previously loaded ) is showing.
Is there any way to kill all the activities which are previously loaded when we press the button in ActC ?
I am new to android and its ruining my time
Please help
thanks in advance
When you launch ActC from ActB, do so with this flag on the intent:
Intent intent = new Intent (this, ActB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Javadoc:
"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."
Just going from ActB to ActC, use Intent and finish() after calling the Intent
Intent intent = new Intent(this, ActC.class);
startActivity(intent);
finish();
And then if you want to go back to B from C, then do the same in reverse, so switch
Intent intent = new Intent(this, ActB.class);
And the rest is the same.
Suppose you move like this
A -> B -> C
All the previous instances will be there in backstack for previous activities.
until and unless it is your requirement to create new instance of activity then only do so.
when you press button in you want to come to B but if you don't need new instance of B you can go with backstack item and according to me you should.
in button click you can simply call onBackPressed() of activity which is called when you press back button of device.
Also as Vee said you can use that flag to clear activities above your current activity.
If you want to "kill" the activity you should call finish();
To achieve your goal you can do the following thing.
When starting ActB from ActA, after calling startActivity(...); put finish();
This way you killed Activity A, do the same in ActB when calling ActC. Then when you call ActB from ActC again, it will start a completely new activity.
If you don't need a new instance of B then you can simply call finish() in your onClick() of C and this will take you back to B and no need for Intent or any other code.
If you need a new instance of B then you can use Vee's suggestion, keeping in mind that this will clear Activities off of the stack if you add more in between.
If you don't need a new instance of B but want to pass data back to it then you can use the flag FLAG_ACTIVITY_REORDER_TO_FRONT
Intent i = new Intent(ActC.this, ActB.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
// can send data to ActB if needed with putExtras()
startActivity(i);
finish(); // if you want to destroy C and take it off the stack
this will not create a new instance of B but bring it to the top of the stack so when you press the "Back" button, you will not have the second instance on there.
When user presses button in ActC to goes back to ActB (by creating a new ActB) do this:
Intent intent = new Intent(this, ActB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will finish both ActC and the previous ActB and create a new ActB.
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);
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.
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