实际开发中通常首页的启动模式都会设为singleTask,当其他栈又回到首页时,此时就会调用onNewIntent方法,此时可根据具体的业务场景使用它。
实例
public class MainActivity extends AppCompatActivity {
private TextView textview;
private static final int REQUEST_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,ThreeActivity.class));
// startActivityForResult(new Intent(MainActivity.this,
// SecondActivity.class), REQUEST_CODE);
}
});
textview = (TextView) findViewById(R.id.textview);
}
@Override
protected void onNewIntent(final Intent intent) {
super.onNewIntent(intent);
Log.e("1","我是three的回调");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String s = data.getStringExtra("bian");
textview.setText(s);
}
}
}
ThreeActivity
public class ThreeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
startActivity(new Intent(ThreeActivity.this,MainActivity.class));
}
}