Issue
This is my second approach to solve this issue, no luck so far.
I've got MainActivity which is a launcher activity.
within its onCreate method I check if static endpoints is initialized.
If no -> start a service and register a receiver (show loader) -> onReceive show some fragments
If yes -> show some fragments
This part works great.
So what is a problem?
I have MainActivity with some fragments. Then I put process into background and do
adb shell am kill com.myapp
PID is killed. So now I want to launch the app again. Android tries to recreate app state meaning firing :
(For sake of simplicity lets assume that MainActivity had only one fragment visible while running)
MainActivity : onAttachFragment()
Fragment : onAttach()
Fragment : onCreate()
MainActivity : onCreate()
Fragment : onCreateView()
...
The problem is this : When MainActiviy.onCreate sees that statics are not initialized then it invokes a call to Service and waits for response. This is a normal start scenario. But this time Fragment is being recreated as well and its lifecycle is mixed with MainActivity lifecycle. Becasuse all is happening async I cannot prevent Fragment's onCreate(), onResume(),onStart(), onCreateView() from firing.. and some of these methods require statics to be initialized (MainActivity did not finish yet the init process) which causes crashes
So my question is : how I can prevent Fragment from being recreated in this scenario? so that this is preferably a clean MainActivity start. Any hints at this point will be appreciated.
Not feasible Solutions or "I Tried it" Solutions :
save static endpoints on the phone -> cannot happen because endpoints change dynamically every 5 mins
singleTop,singleTaskforMainActivity
Solution
I managed to overcome this by doing something like this within MainActivity.onCreate():
if(!MyStatics.isInitialized())
super.onCreate(null);
else
super.onCreate(savedInstanceState);
This causes restart of Activity without recreating fragments
Answered By - AndroidGecko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.