Reputation: 37
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
Upvotes: 1
Views: 485
Reputation: 611
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.
Upvotes: 1
Reputation: 392
what do you mean by
Problem is when device is low on memory, MainActivity is destroyed, while in ActivityA or ActivityB. it is not clear and also
Therefore when Back Button is used
I think that you are talking about the back button press event on the Activity A or B if this is the case , I would suggest you to finish the MainActivity after moving to A or B activity . then on A/B activity , you should over ride the onBackPressed() method , you should start the main activity again and finish activity A/B.
doing this would save device from low memory. BTW did you tried it on real device also? if not then check it on real device and let us know. I hope it would work for you
Upvotes: 0