adp-worker-fragments
adp-worker-fragments copied to clipboard
Problem with retained fragments
I implemented the worker fragment approach in my project based in your 'extra' of this project.
I have Activity A
that has a Fragment A
. That fragment starts a worker fragment.
But, if I start some other activity and come back to Activity A, I get this exception:
java.lang.IllegalStateException: Failure saving state: "WorkerFragment" has target not in fragment manager: "FragmentA"
I googled about this and the workaround for that problem was to put in
onDetach()
of my Fragment A
this: workerFragment.setTargetFragment(null, -1);
But, if now I open another activity and come back to Activity A
, then, rotates
the screen, I get an exception, because now, of course, getTargetFragment() == null
.
So, I added this verification in onAttach()
of Worker fragment: if (getTargetFragment() != null) {}
In this way, I don't get any exceptions, but I need to know: Is this the expected behavior or I'm missing something?
Thanks
Hi, thanks for reporting the issue. Are you able to reproduce this issue in this worker-fragments sample app? I am unable to reproduce it (I'm basically clicking "start", then clicking the home button, then navigating back to the app via the recent apps list). Is this right?
Hi, thanks for the answer. I will try right now exactly with the sample app and post here the results.
I'm not able to reproduce the same error with your project. But, I created a sample project that uses navigation drawer (I think this is the cause of the problem) using the wizard of Android Studio.
In that project, the first option in the drawer is exactly the worker fragment of your project and the second is a simple fragment to start another activity. If you start your Task and then go to the second option in the drawer and open the second activity, you will get the error that I mentioned:
java.lang.IllegalStateException: Failure saving state: TaskFragment{23dcefac #1 task_fragment} has target not in fragment manager: UIFragment{2120a475}
Like I said, I think the problem is because before start another activity, the fragment that starts the worker fragment is replaced by another. But I really don't know how to solve this.
Btw, here is the link for the sample project that I created: https://github.com/luizfp/SampleRetainedDrawer