2015-08-25 00:33:37 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
|
|
|
import android.support.v4.app.FragmentManager;
|
|
|
|
import android.support.v4.app.FragmentStatePagerAdapter;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
public abstract class AbstractFragmentPagerAdapter extends FragmentStatePagerAdapter {
|
2015-11-23 23:04:46 +01:00
|
|
|
private final Set<AbstractGBFragment> fragments = new HashSet<>();
|
2015-08-25 00:33:37 +02:00
|
|
|
private Object primaryFragment;
|
|
|
|
|
|
|
|
public AbstractFragmentPagerAdapter(FragmentManager fm) {
|
|
|
|
super(fm);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object instantiateItem(ViewGroup container, int position) {
|
|
|
|
Object fragment = super.instantiateItem(container, position);
|
|
|
|
if (fragment instanceof AbstractGBFragment) {
|
|
|
|
fragments.add((AbstractGBFragment) fragment);
|
|
|
|
}
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void destroyItem(ViewGroup container, int position, Object object) {
|
|
|
|
super.destroyItem(container, position, object);
|
|
|
|
fragments.remove(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setPrimaryItem(ViewGroup container, int position, Object object) {
|
|
|
|
super.setPrimaryItem(container, position, object);
|
|
|
|
if (object != primaryFragment) {
|
|
|
|
primaryFragment = object;
|
|
|
|
setCurrentFragment(primaryFragment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setCurrentFragment(Object newCurrentFragment) {
|
|
|
|
for (AbstractGBFragment frag : fragments) {
|
|
|
|
if (frag != newCurrentFragment) {
|
|
|
|
frag.onMadeInvisibleInActivity();
|
|
|
|
} else {
|
|
|
|
frag.onMadeVisibleInActivityInternal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|