<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,请问这是为什么
Add one yourself
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
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
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 first
startActivity()
的时候代码已经进入了SingleTopActivity
的生命周期里面去了, 不会马上执行第二个startActivity()
, 代码上看似连续启动了2次SingleTopActivity
, 实际上并没有, 需要你退出SingleTopActivity
返回原来的Activity
.