2

I'm trying to create a fragment that exists for the entire duration of my app's lifecycle. I want it to be created only once and to be destroyed when the activity's onDestroy() function is called (so, ideally, never...). I understand that this goes against what android intended when it introduced fragments, but the nature of my project makes changing this requirement impossible.

What I would like clarification on, is this whole backstack business. I am slightly confused about what exactly the backstack represents, I understand that it is a stack of previous UI states, but does that mean it is a stack of the fragments that the user has currently iterated through? Or is this a stack of FragmentTransactions and when you popBackStack(), it "undo's" the last FragmentTransaction that was committed (and if so, what does it mean to "undo" a FragmentTransaction...does it just remove an added fragment and add a removed fragment, what if I want it to detach a fragment and not remove it when popBackStack() is called?)?

Finally, does calling detach() prevent Android from killing my Fragment (unlike remove() and replace() which will immediately call the onPause(), onStop(), onDestroy(), onDestroyView() sequence)? If so, how do I get popBackStack() to detach() my Fragment (can I even do this?)...should even be adding it to the backstack?

Further restraints on my project - I want all other fragments to behave normally (i.e. they should be able to be destroyed/created at will by the OS). This fragment will not open another fragment, but if I hit the back button, I want it to return to whatever fragment opened it (without destroying the fragment).

Any guidance and/or clarification on the issues I enumerated above would be greatly appreciated!! Thanks in advance!

4
  • 1
    Why is that fragment so important?
    – user
    May 6, 2013 at 18:05
  • we want to integrate our application into other apps. Ours is this fragment that we don't want destroyed. Constantly destroying and recreating it would be a kill on performance and I'm not even sure if it's possible. It's also what is doing the majority of the work of the app and needs to be alive for other fragments to access its data. May 6, 2013 at 18:16
  • If you are doing "work" apart from UI, you probably want to be doing it in a Service. May 6, 2013 at 18:18
  • we do have UI components in our app. But since we are integrating with a customer's own application, we want them to also be able to make calls into our application from their own fragments (for example, they can request something to be shown on our UI via an API call and then bring up our UI). Our app is pretty bulky, so that's why I believe we didn't use a service instead of a fragment. Also, we want to be able to change the state of our application via onStart/Pause/Stop/Resume/etc. Totally possible that I misunderstand services and they would be better, but it's too late for us to change May 6, 2013 at 18:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.