I have A and B activities. A is singleTop on launch, B is excludeFromRecents. When I back from B to A, B is always destroying. How to prevent it? When I press Home then B is just stopped and not destoying. Thanks.
Following back code if B activity:
Intent intent = new Intent(this, ActivityPlaylist.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Without flags back not working at all.
I also have prevention of creating new instance in onCreate of A reason of I launch app from Eclipse (bug):
if (!isTaskRoot()) {
// Android launched another instance of the root activity into an existing task
// so just quietly finish and go away, dropping the user back into the activity
// at the top of the stack (ie: the last state of this task)
finish();
return;
}
Upd: my case is I need do not lose B condition, including list position and so on.
Related
I have few activities.For example A,B,C,D,E,F.
My Home screen is activity A.Now I start Activity B from that and C from Activity B.
So now my stack is A->B->C.
Now if I press back I should be able to navigate in reverse Order.
But If I start Activity D from C.
I want my stack to be A->D as I want to kill B and C.
My expectations is A is my Home screen which should be always there.B and C on having one task and D,E,F having other task.
There is one more catch - I can even start activity D->E->F from A and B from F. In that case when I start B from F, D,E,F should be removed from stack
There are several ways to accomplish this, depending on your application requireents. You should look at using startActivityForResult() in order to start one (or more) activities which can then return a result to the Activity that launched it. This may be a good solution for you.
The other alternative is to use your ActivityA as a kind of "dispatcher. In this case, when C wants to launch D but also clear the task stack back to A first, you can do something like this in C:
Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("launchActivity", "ActivityD");
Using the flags CLEAR_TOP and SINGLE_TOP will cause all activities on top of the existing instance of ActivityA to be finished and cause the Intent to be routed to onNewIntent() of the existing instance of ActivityA.
Now, in ActivityA, override onNewIntent() with something like this:
if (intent.hasExtra("launchActivity)) {
if (intent.getExtra("launchActivity").equals("ActivityD")) {
Intent launchIntent = new Intent(this, ActivityD.class);
startActivity(launchIntent);
} else ...
}
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.
In my app, I have activity A (Launcher/Main Activity) ,B,C. A launches B or C depending on if the user is authenticated.
Now how do I remove A such that when the back button is pressed it just exits the app, not to A. Setting the flag FLAG_ACTIVITY_CLEAR_TOP doesn't seem to work.
#Override
public void onBackPressed{
super.onBackPressed();
finish(); // finish activity B or C
//also finish activity A
}
I think you need to set
android:noHistory="true"
on A. You might also need to launch B/C with these Flags:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Finally, you can finish all activities under the current one in the task stack with the same affinity by calling:
finishAffinity()
For further information: Android: Clear the back stack
In my Android application I'm trying to make sure that certain activity should be always placed at the root of backstack when navigating to it.
For example user starts A->B->C->D activities. Imaging that A - splash screen (with NoHistory = true), B - main page, C,D - some details pages. On details page (D) user has ability to go to the main activity (B). Having B-C-D backstack User press "Go To B" button and navigated to the existing home activity (B), same time C,D activities are stopped and destroyed. I'm using SingleInstance for my B activity and it works fine in this case.
I have case when this doesn't work for me. At the splash screen I can discover that I should skip main page and go directly to one of the details page (for example D). And here when I go to home page my backstack is wrong: D->B instead of just B.
What flags or attributes are more suitable in my case. May be it is more valid approach to update application navigation logic?
Thank you for any suggestions!
In A:
Intent intent = new Intent(B.class, this);
startActivity(intent);
finish(); //finish A behind you so you can only explicitly return to it
In B, two methods for the buttons, I guess, that get you to either C or D:
public void buttonToC(View view){
Intent intent = new Intent(C.class, this);
startActivity(intent);
}
public void buttonToD(View view){
Intent intent = new Intent(D.class, this);
startActivity(intent);
}
In C and D, you can optionally finish the Activity if you press back:
public void onBackPressed(){
Intent intent = new Intent(B.class, this);
startActivity(intent);
finish(); // optional
}
So A -> B and A finishes. From B -> C OR D but B is still running in the background. When in C or D, pressing back brings up the background activity, B. This should work.
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.