Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
/ fragment-stack Public archive

Android library to manage multiple page stacks of Fragment. And it provides life cycles like pages.

Notifications You must be signed in to change notification settings

oniatsu/fragment-stack

Repository files navigation

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

compile 'io.github.oniatsu:fragment-stack:0.1.0'

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().

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.

2. Unregister the FragmentStack

On Activity / Fragment's onDestroy().

FragmentStack.unregister(this);

3. Inherit FragmentPagerLifeCycleFragment on every page Fragments you use.

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.

  • onResumeonForegroundonBackgroundonPause

Add and remove pages, anywhere and anytime.

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)

Add

FragmentStack.of(this).add(new NewFragment());

Remove and back

FragmentStack.of(this).back();

Other flexible page stack APIs are prepared.

Page APIs

Check if it has pages.

FragmentStack.of(this).hasPage();

Back some count times.

FragmentStack.of(this).back(3);

Back to the specific fragment page.

FragmentStack.of(this).back(PastFragment.class, 0);
// or
FragmentStack.of(this).back(PastFragment.class, FragmentManager.POP_BACK_STACK_INCLUSIVE);

Clear all.

FragmentStack.of(this).clear();

Applicaiton global config.

FragmentStack.globalConfig()
        .transitionInterceptor(new FragmentPagerTransitionInterceptor() {
            @Override
            public void onTransaction(PageManager pageManager, FragmentTransaction fragmentTransaction) {
                fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            }
        })
        .setup();

A FragmentStack local config.

FragmentStack.of(this).localConfig()
        .transitionInterceptor(new FragmentPagerTransitionInterceptor() {
            @Override
            public void onTransaction(PageManager pageManager, FragmentTransaction fragmentTransaction) {
                fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            }
        })
        .setup();

Each fragment transaction config.

FragmentStack.of(this).add(new NewFragment(), new FragmentPagerTransitionInterceptor() {
    @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.

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

About

Android library to manage multiple page stacks of Fragment. And it provides life cycles like pages.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages