Reputation: 431
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()
?
Upvotes: 6
Views: 1685
Reputation: 40831
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
.
Upvotes: 7