This is a Android library to manage multiple page stacks of Fragment.
You have a benefit of using the Fragment like just pages easily.
It provides mainly two functions.
- Managing indipendent multiple Fragment stacks like pages.
- Support of adding life cycle like pages. When a Fragment is poped or pushed, onForeground / onBackground is called.
compile 'io.github.oniatsu:fragment-stack:0.1.0'
If you use Activity as Fargment container, write on Activity's onCreate()
.
If you use Fragment as it, write on Fragment's onActivityCreated()
.
FragmentStack.register(this, R.id.fragment_container, new DefaultFragment());
Arguments
- 1st: To be registered Activity / Fragment as page Fragment container.
- 2nd: Fragment container's layout id.
- 3rd: Default Fragment to be initialized first on the container.
On Activity / Fragment's onDestroy()
.
FragmentStack.unregister(this);
public class NewFragment extends FragmentPagerLifeCycleFragment {
// ...
}
It provides two fragment page's life cycles.
- onForeground
- Called when the fragment page become foreground to the user.
- This is called after
Fragment#onResume()
.
- onBackground
- Called when the Fragment page become background to the user.
- This is called before
Fragment#onPouse()
.
The page life cycles is called on this order.
onResume
→onForeground
→onBackground
→onPause
You can get the registered FragmentStack on any class as below.
// If you want to get it on same Activity / Fragment to the page Fragment's container
FragmentStack.of(this)
// or, if you want it on a Activity different from the page Fragment's container
FragmentStack.of(RegisteredActivity.class)
// or, if you want it on a Fragment different from the page Fragment's container
FragmentStack.of(RegisteredFragment.class)
FragmentStack.of(this).add(new NewFragment());
FragmentStack.of(this).back();
FragmentStack.of(this).hasPage();
FragmentStack.of(this).back(3);
FragmentStack.of(this).back(PastFragment.class, 0);
// or
FragmentStack.of(this).back(PastFragment.class, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentStack.of(this).clear();
FragmentStack.globalConfig()
.transitionInterceptor(new FragmentPagerTransitionInterceptor() {
@Override
public void onTransaction(PageManager pageManager, FragmentTransaction fragmentTransaction) {
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
}
})
.setup();
FragmentStack.of(this).localConfig()
.transitionInterceptor(new FragmentPagerTransitionInterceptor() {
@Override
public void onTransaction(PageManager pageManager, FragmentTransaction fragmentTransaction) {
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
}
})
.setup();
FragmentStack.of(this).add(new NewFragment(), new FragmentPagerTransitionInterceptor() {
@Override
public void onTransaction(PageManager pageManager, FragmentTransaction fragmentTransaction) {
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
}
});
If you need to inherit another Fragment and you cannot inherit FragmentPagerLifeCycleFragment
on your page Fragments
Implements FragmentPagerLifeCycleListener
interface and call onForeground
/ onBackground
on onResume
/ onPause
manualy.
public abstract YourFragment extends YourSuperFragment implements FragmentPagerLifeCycleListener {
@Override
public void onForeground() {
}
@Override
public void onBackground() {
}
@Override
public void onResume() {
super.onResume();
onForeground();
}
@Override
public void onPause() {
onBackground();
super.onPause();
}