android - 关于singleTop 并没有调用onNewIntent()
天蓬老师
天蓬老师 2017-04-17 16:13:41
0
5
662
<activity
        android:launchMode="singleTop"
        android:name=".SingleTopActivity"
        android:label="SingleTopActivity">
</activity>
 

点击按钮 跳转2次,

 @Override
        public void onClick(View view) {
           // attemptLogin();
            startActivity(new Intent(LoginActivity.this,SingleTopActivity.class));
            startActivity(new Intent(LoginActivity.this,SingleTopActivity.class));
        }

但是第二次也是 onCreate了并没有进入onNewIntent,请问这是为什么

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(5)
刘奇

Add one yourself

protected void onDestroy() {
        super.onDestroy();
        Log.d("TAG", "onDs");
    }

Log and you will understand. When you start the activity for the first time, the second activity has not started at all. When you exit the first activity, the second activity is started at this time, but at this time the first activity has been destroyed, so the second activity It can only be done with onCreate.

小葫芦

Add this line of code to the Intent in starting the Activity

intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

Remove the line android:launchMode="singleTop"!

小葫芦

When the startup mode is SingleTask, if the first interface is not killed, onNewIntent() will be called only when it is opened for the second time

PHPzhong

If you start SingleTopActivity like this, the top of the stack will still be the current Activity instead of SingleTopAcvitiy. SingleTop mode requires that the activity to be started be on the top of the stack, and then start SingleTopActivity, so that onNewIntent() will be called

小葫芦

@idisfkj is right.
You will start the second time after your firststartActivity()的时候代码已经进入了SingleTopActivity的生命周期里面去了, 不会马上执行第二个startActivity(), 代码上看似连续启动了2次SingleTopActivity, 实际上并没有, 需要你退出SingleTopActivity返回原来的Activity.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template