简体   繁体   中英

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

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() .

Try this:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM