Skip to content

Instantly share code, notes, and snippets.

@gfpacheco
Last active August 29, 2015 14:21
Show Gist options
  • Save gfpacheco/3f272f08fc69da689722 to your computer and use it in GitHub Desktop.
Save gfpacheco/3f272f08fc69da689722 to your computer and use it in GitHub Desktop.
A simple Fragment container meant to be used inside a ViewPager where it can then navigate to other Fragments
package com.gfpacheco.views.widget;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import java.util.concurrent.atomic.AtomicInteger;
@SuppressLint("ValidFragment")
public class CaptainFragment extends Fragment {
private Fragment mCurrentDestination;
private int mFrameLayoutId;
@SuppressLint("ValidFragment")
public CaptainFragment(Fragment firstDestination) {
super();
mCurrentDestination = firstDestination;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FrameLayout frameLayout = new FrameLayout(getActivity());
mFrameLayoutId = ViewUtils.generateViewId();
frameLayout.setId(mFrameLayoutId);
navigate(mCurrentDestination, false);
return frameLayout;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (mCurrentDestination != null && mCurrentDestination.isAdded()) {
mCurrentDestination.setUserVisibleHint(isVisibleToUser);
}
}
// Any child fragment willing to start an activity and wait it's result should
// start it through captains methods `getParentFragment().startActivityForResult()`
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCurrentDestination.onActivityResult(requestCode, resultCode, data);
}
public void navigate(Fragment fragment) {
navigate(fragment, true);
}
public void navigate(Fragment fragment, boolean addToStack) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(mFrameLayoutId, fragment);
if (addToStack) {
transaction.addToBackStack(null);
}
transaction.commit();
mCurrentDestination = fragment;
}
public boolean navigateBack() {
if (getChildFragmentManager().getBackStackEntryCount() > 0) {
getChildFragmentManager().popBackStack();
return true;
} else {
return false;
}
}
public class ViewUtils {
private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
public static int generateViewId() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
for (; ; ) {
final int result = sNextGeneratedId.get();
int newValue = result + 1;
if (newValue > 0x00FFFFFF) newValue = 1;
if (sNextGeneratedId.compareAndSet(result, newValue)) {
return result;
}
}
} else {
return View.generateViewId();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment