Skip to content

Instantly share code, notes, and snippets.

@mootoh
Created February 15, 2017 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mootoh/2652a2a0768340f2537c48b7355a8005 to your computer and use it in GitHub Desktop.
Save mootoh/2652a2a0768340f2537c48b7355a8005 to your computer and use it in GitHub Desktop.
UINavigationController-like stacked fragment
package net.mootoh.tabexperiment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class NavigationControllerFragment extends Fragment {
public static final String KEY_CLASS = "KEY_CLASS";
public static final String KEY_ARGS_FOR_SUB_FRAGMENT = "KEY_ARGS_FOR_SUB_FRAGMENT";
public NavigationControllerFragment() { }
static NavigationControllerFragment newInstance(String rootFragmentClass, Bundle argsForRootFragment) {
Bundle args = new Bundle();
args.putString(KEY_CLASS, rootFragmentClass);
args.putBundle(KEY_ARGS_FOR_SUB_FRAGMENT, argsForRootFragment);
NavigationControllerFragment instance = new NavigationControllerFragment();
instance.setArguments(args);
return instance;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_navigation_controller, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle args = getArguments();
String klass = args.getString(KEY_CLASS);
Bundle argsForSubFragment = args.getBundle(KEY_ARGS_FOR_SUB_FRAGMENT);
if (getChildFragmentManager().getBackStackEntryCount() > 0) // child fragments should be retained
return;
try {
Class kklass = Class.forName(klass);
Constructor ctor = kklass.getConstructor();
Fragment subFragment = (Fragment) ctor.newInstance();
subFragment.setArguments(argsForSubFragment);
getChildFragmentManager()
.beginTransaction()
.add(R.id.container, subFragment)
.addToBackStack(null)
.commit();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (java.lang.InstantiationException e) {
e.printStackTrace();
}
}
public void push(Fragment fragment) {
FragmentManager fm = getChildFragmentManager();
fm.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit();
}
/**
* @return true if popped, false otherwise.
*/
public boolean pop() {
FragmentManager fm = getChildFragmentManager();
if (fm != null && fm.getBackStackEntryCount() > 1) {
fm.popBackStackImmediate();
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment