user799698
user799698

Reputation: 39

Switch between activities

I want to know how can I easily switch between Activities. For example, in my application, I have:

Activity1 -> Activity2 -> Activity3 -> Activity4 -> Activity 5

How can I return to the activity 2 from activity 5 while maintaining the state of activity 2? When I try to start a new intent, I loose the state and the extras inside activity 2...

public void onClick(DialogInterface view, int button) {
            switch (button) {
            case DialogInterface.BUTTON_POSITIVE:


                        Intent i = new Intent(activity,AccueilFournisseur.class);
                        activity.startActivity(i);*/
                        break:
                    }

Thanks a lot for your help

Upvotes: 0

Views: 485

Answers (3)

Nguyen  Minh Binh
Nguyen Minh Binh

Reputation: 24423

Try this:

Intent i = new Intent(activity,AccueilFournisseur.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(i);

Upvotes: 1

Furkan
Furkan

Reputation: 683

Activities are managed by android OS in stack manner. When you finish your current activity (by calling the finish method), your application will automatically turn back to the previous activity (with the state where you left off). In your example, when you already have Activities 1, 2, 3, 4, 5 and try to open a new Activity using intent then the activity stack of your application will be 1,2,3,4,5,2. Instead, you should call the finish method of the activity 5, 4, 3 in order and you'll get what you want. This way, first you will turn back to activity 4, then 3, then 2.

However if you want to go directly back to Activity 2, then as far as I know you should consider writing your own Activity stack manager.

Upvotes: 0

user802421
user802421

Reputation: 7505

You should read Tasks and Back Stack and Managing the Activity Lifecycle. You could change the launch mode of the activities, but I think it's better the leave the launch mode as it is. Save your states in the Activity.onPause() method and restore in Activity.onResume().

Upvotes: 1

Related Questions