Fragment Stack
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.
Installation
Gradle
Basic usage
Setup
1. Register a FragmentStack
If you use Activity as Fargment container, write on Activity's onCreate().
If you use Fragment as it, write on Fragment's onActivityCreated().
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.
2. Unregister the FragmentStack
On Activity / Fragment's onDestroy().
3. Inherit FragmentPagerLifeCycleFragment on every page Fragments you use.
// ...
}
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
Add and remove pages, anywhere and anytime.
You can get the registered FragmentStack on any class as below.
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)
Add
Remove and back
Other flexible page stack APIs are prepared.
Page APIs
Check if it has pages.
Back some count times.
Back to the specific fragment page.
// or
FragmentStack.of(this).back(PastFragment.class, FragmentManager.POP_BACK_STACK_INCLUSIVE);
Clear all.
Applicaiton global config.
.transitionInterceptor(new FragmentPagerTransitionInterceptor() {
@Override
public void onTransaction(PageManager pageManager, FragmentTransaction fragmentTransaction) {
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
}
})
.setup();
A FragmentStack local config.
.transitionInterceptor(new FragmentPagerTransitionInterceptor() {
@Override
public void onTransaction(PageManager pageManager, FragmentTransaction fragmentTransaction) {
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
}
})
.setup();
Each fragment transaction config.
@Override
public void onTransaction(PageManager pageManager, FragmentTransaction fragmentTransaction) {
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
}
});
FAQ
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.
@Override
public void onForeground() {
}
@Override
public void onBackground() {
}
@Override
public void onResume() {
super.onResume();
onForeground();
}
@Override
public void onPause() {
onBackground();
super.onPause();
}