ogaclejapan / SmartTabLayout

A custom ViewPager title strip which gives continuous feedback to the user when scrolling
Apache License 2.0
7.09k stars 1.34k forks source link

Why I can't add a instance of Fragment? #12

Closed ppamorim closed 9 years ago

ppamorim commented 9 years ago

This can be a problem:

add(int title, java.lang.Class<? extends android.support.v4.app.Fragment> clazz)

I need to have a:

add(int title, Fragment fragment)

Why, If I need create a instance of the Fragment, I need use this.

ogaclejapan commented 9 years ago

Hi, @ppamorim

Should use the v13 of utility.

//Optional: see how to use the utility.
compile 'com.ogaclejapan.smarttablayout:utils-v13:1.1.1@aar'
ogaclejapan commented 9 years ago

Sorry mistake.
Do not support because unnecessary instance.

ppamorim commented 9 years ago

@ogaclejapan Based on what you said that?

ogaclejapan commented 9 years ago

Hi, @ppamorim Fragment instance repeat creation and destruction on the ViewPager. So, I think it is better to use the Fragment.instantiate(...) rather than give an instance.

ppamorim commented 9 years ago

You're wrong. Only the view will be recreate, not a instance of the fragment. I use the instance of the Fragment a long time on my applications.

ogaclejapan commented 9 years ago

Please try checked Don’t keep activities in the developer mode. When you view again after returning to the home, it will change to a different instance. Often occurs is similar events in low memory device.


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Object item = super.instantiateItem(container, position);
        Log.d("DEBUG", "position: " + position + " " + item.toString());
        if (item instanceof Fragment) {
            mHolder.put(position, new WeakReference<Fragment>((Fragment) item));
        }
        return item;
    }
ppamorim commented 9 years ago

Well... https://gist.github.com/ppamorim/295d6ef841d3a288ef78

ogaclejapan commented 9 years ago

Hi, @ppamorim Thank you upload the code.

What are the reasons why you think you have the better of this code than the current implementation? I think enough in the current implementation to be instantiated when needed.

The library utility that provides adapters is optional, if you want to use the adapter that you have pre-instantiation, please use to implement yourself.

Although I'm sorry, Function to add a pre-instantiated fragment does not have plans to add to the utility.

ppamorim commented 9 years ago

How to do this?

ppamorim commented 9 years ago

Solved!