Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch tab without detach fragments #11

Closed
xxxifan opened this issue May 5, 2016 · 12 comments
Closed

switch tab without detach fragments #11

xxxifan opened this issue May 5, 2016 · 12 comments

Comments

@xxxifan
Copy link

xxxifan commented May 5, 2016

I found that it will detach fragment once switch tabs, if so, fragments will recreate view while attach and some operation like data load will be performed every time.
can we have a option to not detach/attach just hide/show fragment?

@two1stnamz
Copy link
Contributor

I had this issue too. A way around this is to save the View that gets inflated as in field of your Fragment class. Then in your fragment's onCreateView() method you can check if your View field is null - if it is, inflate it, else return your already inflated View. Similarly, for data load, only load data if your View field is null, else do nothing.

@JadenGu
Copy link

JadenGu commented May 31, 2016

Same issue..

@hauthorn
Copy link

Use the answer from @two1stnamz, or just cache data, and not the entire view.
The idea from the Material Design spec is that the view should reset state.
Caching data and/or some of the state might be a good idea for performance reasons.

@codeteo
Copy link

codeteo commented Dec 12, 2016

Can you elaborate I bit more on : A way around this is to save the View that gets inflated as in field of your Fragment class. ??
Is it different from saving/restoring fragment's state ?

@ncapdevi
Copy link
Owner

Considering adding this as an enhancement moving forward. Reasonably quick to add, but shown/hidden state isn't maintained and would need to be added to the savedInstanceState. If anyone wants to make a PR for this, it would be helpful.

@two1stnamz
Copy link
Contributor

two1stnamz commented Dec 13, 2016

@codeteo Fragment.onCreate() returns a View. When you inflate your View, save it off as a field of your fragment class. So something like this:

public class MyFragment extends Fragment {

   private View cachedView;

   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (cachedView == null) {
      // Inflate and populate
      cachedView = inflater.inflate(R.layout.fragment_layout, container, false);
   }

   return cachedView;
  }

}

@ncapdevi
Copy link
Owner

@codeteo Yes, different then the fragment's state. http://daniel-codes.blogspot.com/2012/06/fragment-transactions-reference.html here's a quick read up on the difference between show/hide and detach/reattach. What is suggested above is to save a field variable of the View, so that you don't have to reinflate the view. When the view is detached, it's view is destroyed, but all of its field variables are maintained. The problem with using "Hide" on a bunch of views is that it can be fairly taxing on memory (why it destroys the view on detach instead of maintaining it) so the developer has to be careful. That said, it shouldn't be up to the library to determine that, and should be up to the developer to decide to detach vs hide.

@hauthorn
Copy link

I agree @ncapdevi. It is a pain to debug memory issues - and this library shouldn't be the cause of any :)

@ncapdevi
Copy link
Owner

@hauthorn @two1stnamz Any suggestions on how this should be implemented. It can either be

  1. A global final setting defined in the constructor
  2. A setting that can be toggled via a setter
  3. A parameter used in every function call. (e.g, push(boolean bDetach))

Not sure what the most common use case is here.

@hauthorn
Copy link

I don't think you should change your code to accomodate this, @ncapdevi.

The extra setting might confuse future developers, and it promotes heavy memory usage (which might trigger an OOM error for the developer later on).

@ncapdevi
Copy link
Owner

@hauthorn Ah! I thought you were requesting it be added as well. Yeah, that's my current thinking as well. Allowing it encourages bad behavior without devs fully realizing what's happening. I'm going to continue to leave it out for now unless a strong case is put forth to add it. Thanks for the feedback.

@francescodagostino
Copy link

For example, SharedElements ReturnTransition does not work for this reason, but there are so many other situations where i'd like to keep previous fragments attacched to my activity.
More than othes, i'd like to have a method param like this
pushFragment(Fragment frag, boolean keepAttached) or similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants