SingleTask启动模式的时候getInent方式取值不更新

先描述一下什么情况下会遇到这个问题:

A界面—->B界面—–>C界面——>A界面,也就是说到达第二个A之前的页面不可以销毁,就是说还可以返回到A,但是经过C页面的操作后还会回到A,并刷新界面;那么我们不能从A界面返回,返回又回到了A吧,这个总感觉怪怪的。所以这个时候就要用到Android的启动模式singleTask,这样就避免了又回到了A的情况。

接下来问题又来了,之前我们都是通过getIntent().getStringExtra(“key”)的方式传值,可是设置了这个启动模式之后,我们发现值不变了,还是一开始的那个,换句话说就是如果启动的activity已经存在,这个时候用getIntent()的到的Intent还是原始的,即Intent的数据没有更新,因为这种模式的时候,会先回调onNewIntent(Intent intent)这个方法,然后onRestart()方法,看到那个参数没—Intent,所以可以通过重写onNewIntent(Intent intent)这个方法来取到新的数据

下面就是模拟一下,并不是按照上面的情况所写:
1、在AndroidManifest.xml中设置AActivity的启动模式

<activity android:name=".intent.AActivity"
            android:launchMode="singleTask"/>

2、启动AActivity直接传值(首次跳到AActivity)

 Intent intent = new Intent(this, AActivity.class);
 intent.putExtra("data", "hahahahha");
 startActivity(intent);

3、为了模拟,从AActivity跳到BActivity

public class AActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a2);
        Log.e("------------->","onCreate");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("------------->",getIntent().getStringExtra("data"));
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e("------------->","------有没有值---:"+intent.getStringExtra("data"));
        Log.e("------------->","---------:"+getIntent().getStringExtra("data"));

    }

    public void clickB(View view){
        Intent intent = new Intent(this,BActivity.class);
        startActivity(intent);
    }
}

4、也是为了模拟,从BActivity跳回AActivity,在看AActivity的取值情况

Intent intent = new Intent(this,AActivity.class);
        intent.putExtra("data","aa");
        startActivity(intent);

5、看Log

07-10 19:25:45.733 4031-4031/com.example.a_0102.mylearn E/------------->: onCreate
07-10 19:25:45.733 4031-4031/com.example.a_0102.mylearn E/------------->: onResumehahahahha

07-10 19:25:52.643 4031-4031/com.example.a_0102.mylearn E/------------->: onNewIntent------有没有值---:aa
07-10 19:25:52.643 4031-4031/com.example.a_0102.mylearn E/------------->: onNewIntent---------:hahahahha
07-10 19:25:52.643 4031-4031/com.example.a_0102.mylearn E/------------->: onResumehahahahha

所以可以看到第二次到AActivity的时候执行了onNewIntent方法,并且可以取到想要的值,在onResume中取得值没有变化。

6、可是这个好像还不是我们想要的情况,看接下来的变形

@Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        //注意这里,设置传来的新的Intent
        setIntent(intent);
        Log.e("------------->","onNewIntent------有没有值---:"+intent.getStringExtra("data"));
        Log.e("------------->","onNewIntent---------:"+getIntent().getStringExtra("data"));

    }

这回看到的log

07-10 19:33:21.063 5078-5078/com.example.a_0102.mylearn E/------------->: onCreate
07-10 19:33:21.063 5078-5078/com.example.a_0102.mylearn E/------------->: onResumehahahahha

07-10 19:33:23.833 5078-5078/com.example.a_0102.mylearn E/------------->: onNewIntent------有没有值---:aa
07-10 19:33:23.833 5078-5078/com.example.a_0102.mylearn E/------------->: onNewIntent---------:aa
07-10 19:33:23.833 5078-5078/com.example.a_0102.mylearn E/------------->: onResumeaa

猜你喜欢

转载自blog.csdn.net/hello_1s/article/details/80990858