ncapdevi / FragNav

An Android library for managing multiple stacks of fragments
1.5k stars 220 forks source link

Any method like this? #55

Closed DanteAndroid closed 7 years ago

DanteAndroid commented 7 years ago

When I use setReenterTransition, I found that reenter transition wasn't shown. Then I find pushFragment may not work as I expect. I use following:

    private void add(Fragment f) {
        activity.getSupportFragmentManager().beginTransaction()
                .hide(this)
                .add(R.id.container, f)
                .addToBackStack("")
                .commit();
    }

I read the source code of PushFragment, found that it detach the current fragment. Why detach? When back pressed, I want to return the old fragment and old state. Any method?

ncapdevi commented 7 years ago

This issue was discussed a bit here #11 . Essentially what it comes down to is hiding a fragment created some pretty significant performance problems if you're not extremely careful, so if you really want to work around it, you can follow the code in that thread. Hope that helps!

DanteAndroid commented 7 years ago

Why do you say that hiding a fragment created some pretty significant performance problems, any reference? Actually, I've already been using code like

   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;
  }

However, my problem is that when using detach, the fragment's onActivityCreated will be executed as well(I have some init logic there) when recreated. Thx again.

ncapdevi commented 7 years ago

http://doublenegative.com/android-fragments-and-memory/ Check out the "What about show/hide?" section. Essentially every aspect of the fragments are held in full, in memory, which isn't ideal.

Yeah, I see where you're running into an issue there, but I don't think that hiding/showing is your best bet to avoid the issue. Is there another way to do what you're doing in OnActivityCreated? If you give me some context as to what you're doing in that method, I could try to offer a suggestion.

DanteAndroid commented 7 years ago

Thx for your link. Actually, I get more confused after reading that (Because I did not even realize fragment has so many methods). I think fragment is so complex. I got one question: The article says:

If you create a lot of fragments, hide them, and add new ones on top (why you would do this I do not know), you will run out of memory sooner or later.

I think it doesn't make sense because when you add one, you won't keep adding all the time. When you press back, the fragment is destroyed. In my condition, I just add one fragment, then no more. When back pressed, it returns to former fragment. Besides, I think hide/show makes fragment responses quickly (There are conditions that user will click and return in a short time).

So what do you say? What's the best practice to use fragment's method?

smihajlovski commented 7 years ago

Hi @DanteAndroid , I also need to show and hide the fragment and not reinstate it each time. Can you please help me, how have you achieved this using the FragNav library? Also, how can I save the view when using DataBinding library?

DanteAndroid commented 7 years ago

@smihajlovski I use android's API:

  private void add(Fragment f) {
        activity.getSupportFragmentManager().beginTransaction()
                .hide(this)
                .add(R.id.container, f)
                .addToBackStack("")
                .commit();
    }

to add fragment. I'm not familiar with DataBinding

smihajlovski commented 7 years ago

@DanteAndroid I've made it with caching the view. Do you have any logic/suggestion when I need to remove the listeners and interfaces in onDestroyView , how would I do it? So it doesn't lead to performance issues?