Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I manage activities like fragments stack

I have many activities.

A is list activity.

B is form activity. And generated dynamically. I open this activity two time in a row.

C is result activity.

A -> B -> B like simple push new activity. if result is success I want to clear all forms when I push C.

A -> B -> B -> C ==> A -> C.

if result is failed when Im in C activity, it can be return different activities like above.

A -> B or A -> B -> B

I use cleartop when I push C but it clear all activities how can I save A activity's state.

How can I manage activities like fragments.

* When I back from second B, first B should open *

like image 607
6155031 Avatar asked Feb 04 '26 23:02

6155031


2 Answers

You can achieve it by following below steps:

  1. Set android:launchMode="singleTask" of ActivityA in AndroidManifest.xml file.
  2. Set onNewIntent method in ActivityA like below:

    @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Bundle mBundle = intent.getExtras(); if(mBundle!=null){ String launchActivity = mBundle.getString("activityName"); switch (launchActivity){ case "ActivityD": // This is Activity Name Here it is ActivityD.class startActivity(new Intent(ActivityA.this, ActivityD.class)); break; }
    }
    }

  3. Now Start ActivityA from ActivityC like below.

    startActivity(new Intent(ActivityC.this, ActivityA.class).putExtra("activityName", ActivityD.class.getSimpleName()));

It will call onNewIntent method of ActivityA and will match the argument and launch ActivityD from ActivityA. So your ActivityA will remain in stack and ActivityD will be added in stack on Top.

  1. Now to achieve A -> B from A -> D you can call finish() method in ActivityD when you start ActivityB from ActivityD.

Regarding ActivityLaunchMode please refer this link

Hope it will work for you!

like image 55
Seeya Avatar answered Feb 06 '26 12:02

Seeya


On Activity A you cant goto Activity B using this

    startActivity(new Intent(Activity_A.this, Activity_B.classs));

From B to C

    startActivity(new Intent(Activity_B.this, Activity_C.classs));
    finish();

From C to D

    startActivity(new Intent(Activity_C.this, Activity_D.classs));
    finish();

From D to A

    finish();

It will close Activity_D and Resume() Activity_A

like image 20
Ali Ahmed Avatar answered Feb 06 '26 12:02

Ali Ahmed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!