Android Clear Middle Activities - java

I have 5 Activities,
A - Base Activity
B,C,D - Normal Activities
E - Final Activity
A-B-C-D-E
I navigate from A to B to C to D, D takes me to E. I want behaviour : if I press back button on D it should take me to C , back button from C should take me to B down to A. But if I have moved to Activity E from D, I want the back button from E it should take me to A skipping B,C and D.

In E:
#Override
public void onBackPressed() {
Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
}
This will go back to A, finishing B, C, D and E.
Using FLAG_ACTIVITY_SINGLE_TOP will ensure that you return to the existing instance of A. If you want to also finish the existing instance of A and create a new one to return to, simply remove FLAG_ACTIVITY_SINGLE_TOP.

You can use a static variable to track which activity you're coming from. Then override onBackPressed to check that variable and move to the appropriate activity. Something like:
public static Boolean skipActivities = false;
Then when you start activity E from D set it to true. For your activity E:
#Override
public void onBackPressed() {
if (skipActivities){
//start activity A, skipActivities should be reset to false also
} else {
super.onBackPressed();
}
}

you can "group" you activities, by using affinity.
So in Manifest file add android:taskAffinity="activity.normal" to activities B,C,D,E. And now you'll be able to finish all this group in any of this activities by calling finishAffinity() (instead of usual finish()) in your case you can just add to E activity:
#Override
public void onBackPressed() {
finishAffinity();
super.onBackPressed();
}

Related

Closing current and previous activity on click of button(not the back button) on current activity

I start in main activity A. From A I go to activity B and from B I go to C. Now in activity C if a button is clicked (not back button) ,activity B and C need to close and a new instance of activity B needs to be created. But if that particular button is not pressed and back button is pressed then activity C is closed and we return to previous activity B. I am making a reminder app. Activity B is a recycler view showcasing all the tasks and Activity c is to create new task. So if on activity C submit button is pressed I want activity C and B to be closed and a new instance of b to be opened i.e. recycler view with new tasks but if submit is not pressed I want to close activity C and return to original instance of B. How to do so?
Maybe you can use this 2 methods
(1) Use IntentReceiver
From second activity
Intent i = new Intent(SecondActivity.this, ThirdActivity.class);
i.putExtra("SecondActivity", new ResultReceiver(null) {
#Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
SecondActivity.this.finish();
}
});
startActivityForResult(i, 1);
In third activity
((ResultReceiver) Objects.requireNonNull(getIntent().getParcelableExtra("MainActivity"))).send(1, new Bundle());
//And after this finish current activity (Second activty)
startActivity(new Intent(ThirdActivity.this, FirstActivity.class));
finish();
(2) If Clear all previous and current activity
Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
You can use the method finishAffinity() to exit an activity. I hope this at least makes the problem a bit easier.
Start Activity B with the intent flag FLAG_ACTIVITY_CLEAR_TOP
https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TOP

2 activity android

I have 2 activity and when i go to A from B and then i press "back" it creates antoher activity but i don't return to the same
When you go activity A to activity B. Just use startActivity(yourIntent) without finish(). Like-
Intent goNext = new Intent(A.this, B.class);
startActivity(goNext );
Then when you go back from activity B to activity A, just use finish() method on your activity B. Like -
#Override
public void onBackPressed(){
finish();
}
OR
You can use startActivityForResult()

How to synthetically add activity to back stack before starting another one?

Let's say I'm in activity A. I want to start activity B, but I want the user to be taken to activity C if they press back on activity B.
So even though the user sees A -> B, I want it to be A -> C -> B.
I know I could use TaskStackBuilder and synthetically create the ABC stack. However, sometimes A isn't simply one activity; there might be some previous navigation the user did that I don't want to lose and that would be too much trouble to synthetically recreate with TaskStackBuilder.
Is there a way to use TaskStackBuilder keeping the current back stack? Or is there any other way to synthetically add an activity to the back stack before starting another one?
What you may do is override onBackPressed() in Activity B, from where you will launch activity C and finish activity B. From user's perspective he will see:
A -> B -> (back press event) -> C
In backstack:
A -> AB -> AC
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(this, ActivityC.class);
startActivity(intent);
finish();
}

Prune back history after completing activity

I have the following activity path:
Main -> A0 -> A1 -> A2 -> B
or
Main -> B
On Main, the user can select to either show something on B, or to create something new with the A series.
After completing the A series, it goes to B.
When the user gets to B via the A route, I want the back button to go to Main. But when the user is in the A series, I want the back button to go to the previous A (or Main from the first A).
I tried using FLAG_ACTIVITY_NO_HISTORY when I create the intents, but that just makes everything go back to Main.
Is there a way to mark the activities for removal once I hit a threshold?
You could add a BroadcastReceiver in all activities you want to close (A0, A1, A2):
public class MyActivity extends Activity {
private FinishReceiver finishReceiver;
private static final String ACTION_FINISH =
"com.mypackage.MyActivity.ACTION_FINISH";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finishReceiver= new FinishReceiver();
registerReceiver(finishReceiver, new IntentFilter(ACTION_FINISH));
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(finishReceiver);
}
private final class FinishReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_FINISH))
finish();
}
}
}
To close these activities trigger the following broadcast from Activity B.
sendBroadcast(new Intent(ACTION_FINISH));
Here is the github example project for the same.
Define the logic in this very way
In Series of Activity A define the logic in this very way
public static Activity A=null;
//in onCreate();
A=this;
//Same way in B
public static Activity B=null;
B=this
now wherever you want the back condition to be checked override the OnbackPressed.
#Override
public void onBackPressed()
{
if(Activity_A.A!=null)
{
finish();
return;
}
if(Activity_B.B!=null){
//your condition
finish();
}
}
You can fire a new intent for Main activity with SINGLE_TOP and CLEAR_TOP flags in onBackPressed of B. So no matter how you landed at B, pressing back takes you to Main with all history cleared. No need to mess with A series activities.

Intent.FLAG_ACTIVITY_CLEAR_TOP in SherlockActivity not serving the purpose

The Scenario :
I have four activities : A, B, C and HomeActivity. A is my launcher activity. Am using actionbarSherlock, so A,B and C have menu option in the bar.
The flow is :
A-> B-> C --**On submit in C**--> HomeActivity
Now when i press Back button on Home Activity, it goes back to activity B as after clicking Submit in C , am using
Intent intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP ); //Shouldn't this clear A,B and C ??
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
startActivity(intent);
finish();
But i would like to remain on HomeActivity only as data is submitted and then it was started.
Also if menu button is pressed on A,B,C, then HomeActivity is started and in that case, i would like to have default behaviour of Back button(i.e go back to activity in which menu was pressed)
Any insights on how to do this as FLAG_ACTIVITY_CLEAR_TOP not serving the purpose!
(P.S. : HomeActivity is not a launcher activity)
I think you finish your Home Acitivity while you go to the Home Activity -> Activity A. When you are using clear top flag then your Home Activity should be alive in your stack. Please make sure that you are not finish your Home activity.
And also put this code onKeyDown() method in your Home Activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.e("onkeyDown>>>>", "Called>>>>>");
finish();
}
return super.onKeyDown(keyCode, event);
}
check more details check this link:
Other Way
EDIT:
try to put above code into Activity A.
remove finish() method from your code like below:
Intent intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP ); //Shouldn't this clear A,B and C ??
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
startActivity(intent);
And Most Important thing Don't Finish your Activity A launcher
Activity for Clear top method it should be in stack of Activity remain
present in your device.
Hope it will solve your problem.

Categories