Reputation: 502
I am with an application that is using the NFC to write and I have been watching many tutorials and in all these they use the onNewIntent method in an activity. I am using fragments and I have two questions:
How can I use this method in a fragment or how should I do it? And the other question is, what does this method do in the case of the NFC?
Upvotes: 1
Views: 203
Reputation: 13865
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the Intent#FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.
Check the Android docs.
It means if you have an Activity like the one mentioned above, and you have that Activity created and present in the stack of your app, then when that Activity needs to be re-launched (for instance, through a deep linking URL), then a new Activity won't be created. Instead, the Activity instance in the stack will be moved to top, and onNewIntent
of that Activity will be called with the new Intent that was used to create that Activity.
So, if someone clicks on multiple deep linking URLs continuously, you won't have a bunch of instances of the same Activity on top of each other. Instead, you will have only one Activity instance with the latest Intent.
I don't have experience in NFC. I'm assuming, similar to deep linking, you will receive some data from outside the app (maybe from the sensor) to the app, and you use one Activity to receive and handle that data. In that case, you need to use onNewIntent
.
Since only an Activity can be launched from externally, this onNewIntent
is available only for Activity. If you are using Fragment, you should pass the data from Activity to your Fragment after receiving latest data in onNewIntent
.
Upvotes: 1