3

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 *

6155031
  • 4,171
  • 6
  • 27
  • 56
  • I would recommend watching this video; https://youtu.be/MvIlVsXxXmY What you want can be done easily with fragments. Here is another related doc you should definitely read. https://developer.android.com/guide/components/activities/tasks-and-back-stack – Efe Budak Nov 22 '18 at 09:21

4 Answers4

4

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!

Seeya
  • 91
  • 2
2

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

Ali Ahmed
  • 2,130
  • 1
  • 13
  • 19
2

When you startActivity from Activity B to Activity C or C to D you need to call finish(); expect for Activity A. Like A -> D.

Start Activity(new Intent(A.this,D.class));

Now, when you click Activity A --> B then B--> C and then C--> then you should do like

A -> B.

Start Activity(new Intent(A.this,B.class));

B --> C

Intent intent =new Intent(B.this,C.class);
startActivity(intent);
finish();

C --> D

Intent intent =new Intent(C.this,D.class);
startActivity(intent);
finish();

Now, When you press back you will be return to activity A. You should remove all clearActivityTop(); from code.

Apple Appala
  • 329
  • 4
  • 16
1

You can achieve that by simply using fragments more often

Activity:: When an activity is placed to the backstack of activities the user can navigate back to the previous activity by just pressing the back button.

Activity can exist independently.

Fragment:: When an fragment is placed to the activity we have to request the instance to be saved by calling addToBackstack() during the fragment transaction.

Fragment has to live inside the activity source

By using fragment you can easily remove any page you want by using tags, however if you still want to use activity

if you use startActivityForResult() instead of startActivity(), then depending on the return value from your activity, you can then immediately finish() the predecessor activity to simulate the behaviour you want. By using this method in all your activities, you can have this behaviour cascading the activity stack to allow you to go from activity D to activity A.

Hossam Hassan
  • 795
  • 2
  • 13
  • 39
  • Another thing you can do is to use broadcast which close certain activity, for example you will need to set broadCastReceiver in each activity, if you need the code of broadcast let me know (this way you can close Any activity from ANY place in your app) – Hossam Hassan Nov 22 '18 at 09:48