How to close Activities

2086 views android
0

I have 4 Activities and each one has different objects which can get from server. The main purpose is when I submit final data in activity 4, Activity 1 must be called again.

App Activity cycle

At this time I have to close activity B and C. If I just finish activity D and start Activity A in a new intent, the previous Activities are alive and whenever I want to come back from app I can see those activities again. How can I close multiple activities at the same time?

Final purpose

answered question

After go D to A. You need to go back on D. When user press back button ?

6 Answers

8

for going back from activity D to activity A pass the intent in onBackPressed() method.

@Override
     public void onBackPressed() {
            Intent in = new Intent(Activity-D.this,Activity-A.class);
            startActivity(in);
            finish();
        }

posted this
3

Start Activity A from Activity D with the flags FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK to clear the stack when creating the new Activity.

posted this
1

have u used the following flag

Intent.FLAG_ACTIVITY_NEW_TASK

Intent intent = new Intent(CLASS_D.this, CLASS_A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

posted this
12

Use the Intent Flags to clear the Stack ,

 Intent intent = new Intent(D.this,A.class);  
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);

Then the Activity A only will be in the Stack

posted this
7

Use FLAG_ACTIVITY_CLEAR_TOP Intent Flag to start activity A again using -

Intent intent = new Intent(ActivityD.this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

This flag will clean up B and C from the stack and open A.

posted this
9

You can use EventBus to do that.

Define a event class FinishActivity

public class FinishActivity{

}

register event in activity B,C

you can use

EventBus.getDea
fault().register(this)

in your onCreate method for registering and unregister it in onDestroy method.Create one method

public void onMainThread(FinishActivity event) {
     finish();
}

in your activityB,C.You can send FinishActivity event by EventBus.getDefault().send(new FinishAxtivity());Activity B,C will finish themselves after sending that event.

posted this

Have an answer?

JD

Please login first before posting an answer.