I'm running into an unpleasant issue I don't know how to solve properly.
Scenario:
MainActivity has a method handleIncomingIntent()
This method parses the Extras coming in the incoming intent (for service, or broadcast receiver) and opens Child Activities based on an intent data. So, when the incoming Intent has data of type A it will startActivity(ActivityA.class), if of type B then startActivity(ActivityB.class) if no data it will stay in MainActivity
Problem is when device is low on memory, MainActivity is destroyed, while in ActivityA or ActivityB.
Therefore when BackButton is used - the MainActivity gets restored and it's incoming Intent is restored at the same state as before handling it despite the fact I'm doing incomingIntent.removeExtras(KEY) in the end of my handleIncomingIntent() method. So the outcome is - it's starting the Child Activity once again and it's a loop!
I realize that I can store some isIntentConsumed flag into memory inside onDestroy() and then read it restoreSavedState() and use it to dismiss the intent since it's already consumed.
I just feel there must be a better way than a "bandaid" that I just described.
Kind Regards, Pavel
If the stopped Activity is destroyed due to system constraints other than normal conditions(user presses back or activity finish itself), the method onSaveInstanceState(Bundle savedInstanceState) will be called. When user navigates back to this kind of activity, onRestoreInstanceState(Bundle savedInstanceState) will be called and the previous saved bundle will be passed as parameter to both onRestoreInstanceState() and onCreate().
So you can check the actual parameter of onCreate(Bundle savedInstanceState), if savedInstanceState != null
you can know the activity is recreated. Hope helped.