简体   繁体   中英

NFC intent calling onPause before onNewIntent - How to know the origin of the onPause call?

While scanning an NFC tag in an application with Foreground Dispatch enabled, onPause() is called before onNewIntent() . Is it possible to get the NFC intent already in onPause() so that I can stop (or not) processes according to the origin of the intent?

Since the activity is in the foreground and should not be paused/resumed when a tag is scanned, and since I have tasks that have to stop or start in onPause / onResume , you can see the problem.

The getIntent().getAction() is always "MAIN", but surely there must be a way to get a more accurate description of the intent? Or is not possible to know it's an NFC intent before entering onNewIntent() ?

No, you won't be able to know what the new intent is before it is delivered to your activity in onNewIntent() . That's the point of the onNewIntent() callback: Deliver the information about the new intent to your activity. Only when this method is called, you can obtain the new intent through the intent parameter of that method:

@Override
protected void onNewIntent(Intent intent) {
    ...
}

Note that getIntent() will always return the intent that initially started your activity unless you explicitly update that intent using setIntent(newintent) . So if you started the activity through the launcher you will get the intent action MAIN.

onPause / onResume() is called due to the way the NFC service dispatches the intent to your activity. There is nothing you can do about that. In fact that should not be something that you need to differentiate in your application logic. If onPause is called you should do whatever you would normally do in there regardless of whether this was invoked due to explicit user UI interaction moving some other UI component in front of yours or due to the system moving some (invisible) UI component in front of yours. If your current onPause logic really needs to differentiate between the two, then it might probably better be done in onStop .

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