In my main activity, onCreate I set a textView to the current date. From that activity, I startActivityForResult activity2, where I can change the date. onActivityResult, I set the textView to whatever date was returned.
Now, if you close the main activity and the textView is not the current date. The next time you open it (from the button on home screen or app drawer), I would like it to set the textView to the current date.
After it opens for the first time, the next time you open it, it doesn’t hit onCreate. I’ve tried using onResume however this prevents activity2 onActivityResult from changing the date.
How about something like this:
First of all, create an
onSaveInstanceState(Bundle)method and save the current contents of the dateTextViewto it. This is called before Android stops, hides, or destroys your activity.When you start activity2, your
onStop()of activity1 will be called. In there, set a flag to indicate whether or notonStop()is called due to you starting a new activity (make the intent for activity2 a member, not local variable for this). Ensure you save this flag inonSaveInstanceStateof activity1.Implement
onRestoreInstanceState(Bundle)in activity1, and restore to local variables the date and flag values.Finally, in
onResumeof activity1, check the flag – it you are resuming because you are returning from activity2, do not change the TextView since that will be set inonActivityResult, otherwise, you can safely restore the saved date value.