简体   繁体   中英

Getting Intent data regardless of status of receiving activity

I have a an activity that sends data to another in the usual android way (by using putExtras)

i.putExtra("ID_NUMBER", id);

But I have an activity that can be either closed or in the background when this intent is sent. I am using the following method to catch the intent when my activity is in the background:

protected void onNewIntent(Intent intent) {
    String id = intent.getStringExtra("ID_NUMBER");
}

But I've noticed this method does not get called when the activity is created. Is there a catch all method that intents pass through when entering an activity or is the standard way of doing this to get the Intent in OnCreate()?

Extract the code to react on new intent and put it also into onCreate :

@Override
protected void onCreate(...) {
    doOnNewIntent( getIntent() );
}

@Override
protected void onNewIntent(Intent intent) {
    doOnNewIntent( intent );
}

private void doOnNewIntent(Intent intent) {
    String id = intent.getStringExtra("ID_NUMBER");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM