public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
processExtraData();
}
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);//must store the new intent unless getIntent() will return the old one
processExtraData()
}
private void processExtraData(){
Intent intent = getIntent();
//use the data received here
}
OnNewIntent 的调用:
单例模式(SingleStask )的Activity 当遇到新的Intent的时候,
单顶模式(singleTop)
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//原先已经存在这个即将跳到的Activity;
延伸---可以使用这个标志进行刷新Intent,进而刷新当前界面
不会走onCreate方法了,
而是走OnNewIntent方法,接着是生命周期其他方法;
<activity android:label="@string/app_name" android:launchmode="singleTask"android:name="Activity1"/>
所以在处理的是时候,OnNewIntent 的常用方法,是和OnCreate方法一样。
@Override
protected void onNewIntent(Intent intent) {
MyLog.d(TAG, TAG+"______ onNewIntent 0"+getIntent().getBooleanExtra(ComConstants.isAllowedSendVoiceMsg, isAllowedSendVoiceMsg));
MyLog.d(TAG, TAG+"______ onNewIntent 0-1"+intent.getBooleanExtra(ComConstants.isAllowedSendVoiceMsg, isAllowedSendVoiceMsg));
setIntent(intent);//注意 是设置的是intent 不是设置getIntent()
MyLog.d(TAG, TAG+"______ onNewIntent 1"+getIntent().getBooleanExtra(ComConstants.isAllowedSendVoiceMsg, isAllowedSendVoiceMsg));
super.onNewIntent(intent);
MyLog.d(TAG, TAG+"______ onNewIntent 2"+getIntent().getBooleanExtra(ComConstants.isAllowedSendVoiceMsg, isAllowedSendVoiceMsg));
}