Any intent that gets passed either when an activity is created or as a param to onNewIntent()
, lives as long as activity has not been destroyed. Well, even if the activity is launched from recent apps section after being destroyed but that's another issue.
My question is what should be the best practice in such cases to avoid duplicate processing due to old intent when activity is started/resumed from background or 'created' from recent apps section.
Say, am pulling getDataString()
for example for analytics which should ideally be tracked only when the app has actually started via a deeplink
. But it's very much available each time in the call-chain of onStart()
, inside the old intent. What is recommended?
onStop()
? //seems most logical to me. pitfalls?onStop()
to identify if it's an old one?After trying out various cases, here is what I found. Some of these opinions may have best suited my codebase but am guessing they are generically applicable.
Similar to above but much cleaner. Stored a field in intent itself indicating if it has been 'consumed' [for any processing]. Function looks like:
private void markIntentValuesTracked(final boolean status){ if(getIntent() != null){ getIntent().putExtra(LAUNCH_INTENT_VALUES_CONSUMED, status); } }
method calls:
onCreate():
boolean isOldIntent = (getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0;
markIntentValuesTracked(isOldIntent);
onNewIntent(): markIntentValuesTracked(false);
onStop(): markIntentValuesTracked(true)