singleTask改版方式
摘自伯乐文章http://android.jobbole.com/82335/
RS中注册广播略显麻烦,确实,有人提出了下面这种方法:
- 设置MainActivity的启动模式为singleTask
android:launchMode="singleTask"
- 重写MainActivity的onNewIntent()方法并添加一个TAG_EXIT值
public static String TAG_EXIT = "exit";
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if(intent != null){
boolean isExit = intent.getBooleanExtra(TAG_EXIT, false);
if(isExit){
this.finish();
}
}
}
3、在需要结束应用的时候进行如下操作
Intent mIntent = new Intent(SecondActivity.this, MainActivity.class);
mIntent.putExtra(MainActivity.TAG_EXIT, true);
startActivity(mIntent);
这种方法相对来说较为方便简单。