Today I’m going to introduce you to an open source library. After reading this library, many fragments and status bar problems will be gone.

I don’t like to be bothersome, so let’s get to the point.

Do the fragment thing

What are the problems with fragments?

Animation is not working properly, especially with fragments nested?

Can not perform this action after onSaveInstanceState exception?

Abnormal ghosting?

Annoying life cycle?

Don’t know which fragmentManager to use?

AndroidNavigation to save the world

Download and experience APK

You are advised to use Android 4.4, 5.0, and 6.0

One line of code to achieve Fragment nesting, one time to build a nested hierarchy

AndroidNaviation provides several commonly used containers to help us quickly implement fragment nesting, respectively DrawerFragment, TabBarFragment, NavigationFragment

The DrawerFragment is a fancy drawer that hides the status bar automatically when opened.

Trust me, your designers will love it.

TabBarFragment provides us with the BottomNavigationBar TAB ability, such as wechat, Alipay home page at the bottom of one

NavigationFragment manages its sub-fragments in the form of a stack and provides transition animations. Sub-fragments nested within the NavigationFragment are blessed with the ability to automatically create toolbars and automatically add back buttons when appropriate.

If the home page of an application has both drawers and tabs, and each TAB page has to be able to switch to other pages, it looks like a very complicated nesting. But AndroidNavigation can build these UI hierarchies all at once

// MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {
        / / home page
        HomeFragment homeFragment = new HomeFragment();
        NavigationFragment homeNavigatoinFragment = new NavigationFragment();
        homeNavigationFraggment.setRootFragment(homeFragment);
        homeNavigatoinFragment.setTabBarItem(new TabBarItem(R.drawable.icon_home, "Home page"));
        
        / / address book
        ContactsFragment contactsFragment = new ContactsFragment();
        NavigationFragment contactsNavigationFragment = new NavigationFragment();
        contactsNavigationFragment.setRootFragment(contactsFragment);
        contactsNavigationFragment.setTabBarItem(new TabBarItem(R.drawable.icon_contacts, "Address book"));
        
        // Add TAB to TabBarFragment
        TabBarFragment tabBarFragment = new TabBarFragment();
        tabBarFragment.setFragments(homeNavigatoinFragment, contactsNavigationFragment);
        
        // drawer
        DrawerFragment drawerFragemnt = new DrawerFragment();
        MenuFragment menuFragment = new MenuFragment();
        drawerFragment.setMenuFragment(menuFragment);
        drawerFragment.setContentFragment(tabBarFragment);
        drawerFragment.setMaxDrawerWidth(300); // Set the maximum width of the menu
        
        // Set the DrawerFragment to the root of the ActivitysetRootFragment(drawerFragemnt); }}Copy the code

So, we’ve built the UI hierarchy that we mentioned above, and there are four layers of fragments nested in it. Is that scary or exciting?

These containers aren’t good enough for your needs? Custom containers!

Our ViewPagerFragment is a custom container

The core code

// ViewPagerFragemnt.java

int location;

@Override
public boolean isParentFragment(a) {
    return true;
}

@Override
protected int preferredStatusBarColor(a) {
    int[] colors = new int[] {Color.RED, Color.GREEN, Color.BLUE};
    return colors[location];
}

private void initView(View view) {
    AppBarLayout appBarLayout = view.findViewById(R.id.appbar_layout);
    // important
    if(isStatusBarTranslucent()) {
        appendStatusBarPadding(appBarLayout, -2); }}Copy the code

Is not complicated

One line of code to achieve Fragment jump, no longer need to write a lot of code to operate Fragment, do not worry about using the wrong FragmentManager

All fragments have two basic navigation capabilities: presentFragment and dismissFragment, which is to open and close a fragment

// HomeFragment.java
presentFragment(new TargetFragment(), REQUEST_CODE);
Copy the code

One line of code is skipped

With a request code, the TargetFragment can return results to the previous page via setResult before closing

 Bundle result = new Bundle();
 result.putString("text", resultEditText.getText().toString());
 setResult(Activity.RESULT_OK, result);
 dismissFragment();/ / close
Copy the code

If fragemnt is nested in a NavigationFragment, there will be more navigation capabilities, such as

  • Push an interface
  • Pop out of stack an interface
  • PopTo stacks multiple interfaces to the specified interface at the same time, and don’t worry about stacks of multiple interfaces at once, animation will have problems

Not happy with the navigation? Custom navigation!

For details, see the GridFragment class in demo

Lazy loading

AndroidNavigation provides two lifecycle functions to help us implement lazy loading

protected void onViewAppear(a);
protected void onViewDisappear(a);
Copy the code

Take care of the status bar stuff

What are the status bar annoyances?

Immersion and immersing bugs?

Black letters on a white background but nothing under 5.0 is white?

Want to make immersion and status bar colors compatible with 4.4?

What, the keyboard doesn’t work?

One line of code switches the immersive status bar, is compatible with Android 4.4 and fixes bugs

// Open immersion
setStatusBarTranslucent(true);
Copy the code

Others, such as status bar colors, simply override the following methods selectively to return the expected result

// Status bar font style, black or white
protected BarStyle preferredStatusBarStyle(a);
// Whether to hide the status bar
protected boolean preferredStatusBarHidden(a);
// Status bar color
protected int preferredStatusBarColor(a);
// Whether the transition is smooth when switching the status bar color
protected boolean preferredStatusBarColorAnimated(a);
Copy the code

It’s that simple

For a more detailed description of AndroidNavigation’s more features, see README here

Give a star if you like, because you need 100 to summon the dragon.