简体   繁体   English

Android使用先前的包/附加功能返回到先前的活动

[英]Android go back to previous activity with previous bundle/extras

I have Activity #1: and I move to #Activity 2 我有活动1:然后转到活动2。

Activity1: 活动1:

Intent PostLogin = new Intent(getApplicationContext(), activity2.class);
        PostLogin.putExtra("email",emailOfUser);
        PostLogin.putExtra("token", authToken);
        PostLogin.putExtra("url", url);
        startActivity(PostLogin);

Activity #2: 活动2:

ONCREATE ACTIVITY 2:


Intent loginActivity = getIntent();
        token = loginActivity.getStringExtra("token");
        email = loginActivity.getStringExtra("email");
        url = loginActivity.getStringExtra("url");


MOVING TO ACTIVITY 3

 Intent createOrJoin = new Intent(getApplicationContext(), activity3.class);
            createOrJoin.putExtra("token", token);
            createOrJoin.putExtra("url",url );
           // createOrJoin.putExtra("token", );
            startActivity(createOrJoin);

ACTIVITY 3 MOVING BACK TO ACTIVITY 2 活动3返回活动2

Intent backtoPost = new Intent(getApplicationContext(), activity2.class);
                        startActivity(backtoPost);
                        finish();

So I initially have info in Activity 2 I got from Activity 1. Then from Activity 2 I go to Activity 3, where after some stuff I come back to Activity 2. 因此,我最初在活动2中获得了从活动1获得的信息。然后从活动2中进入了活动3,在此之后,我又回到了活动2。

How can I still have the values of token, email, url ? 我如何仍可以拥有token, email, url的值?

There are multiple ways of doing this. 有多种方法可以做到这一点。

  1. One simple way is that you can have them in a variable. 一种简单的方法是可以将它们放在变量中。

     public class YourActivity extends Activity { String email; public void onCreate(Bundle savedInstanceState) { super.onCreate(); // Set content view etc email = getIntent().getStringExtra("email"); } } 
  2. Another better way is to save those variables that you need when onSaveInstancestate is called and then restore them in onCreate or onRestoreInstance . 另一个更好的方法是保存在调用onSaveInstancestate时所需的那些变量,然后将其还原到onCreateonRestoreInstance Check following questions for details: onSaveInstanceState () and onRestoreInstanceState () and When are onSaveInstanceState() and onRestoreInstanceState() called, exactly? 请检查以下问题以获取详细信息: onSaveInstanceState()和onRestoreInstanceState()以及何时准确调用onSaveInstanceState()和onRestoreInstanceState()? for how to use them. 有关如何使用它们的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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