开发者

Switch between activities

开发者 https://www.devze.com 2023-03-22 16:20 出处:网络
I want to know how can I easily switch between Activities. For example, in my application, I have: Activity1 -> Activity2 -> Activit开发者_开发百科y3 -> Activity4 -> Activity 5

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

Activity1 -> Activity2 -> Activit开发者_开发百科y3 -> 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号