Skip to content

Instantly share code, notes, and snippets.

View danielwilson1702's full-sized avatar

Daniel Wilson danielwilson1702

View GitHub Profile
@danielwilson1702
danielwilson1702 / fragmentSwap.java
Last active September 5, 2015 11:39
Swapping a fragment - standard
Bundle args = launchingFragment.getArguments();
if (args == null) {
args = new Bundle();
}
if (mMyObject != null) {
args.putSerializable(Constants.Args.MY_OBJECT, mMyObject);
}
Fragment myNiceFragment = MyNiceFragment.instantiate(mContext, MyNiceFragment.class.getName(), args);
@danielwilson1702
danielwilson1702 / OnBackPressed.java
Created September 5, 2015 11:53
OnBackPressed interface
public interface OnBackPressed {
void onBackPressed();
}
@danielwilson1702
danielwilson1702 / BaseFragment.java
Created September 5, 2015 11:55
A base fragment with onBackPressed()
public class BaseFragment extends Fragment implements OnBackPressed {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
@danielwilson1702
danielwilson1702 / MyActivity.java
Last active September 5, 2015 12:10
Activity hosting fragments with back button listeners
@Override
public void onBackPressed() {
tellFragments();
super.onBackPressed();
}
private void tellFragments(){
List<Fragment> fragments = getSupportFragmentManager().getFragments();
for(Fragment f : fragments){
if(f != null && f instanceof BaseFragment)
@danielwilson1702
danielwilson1702 / FragmentWithBackImpl.java
Created September 5, 2015 12:17
Fragment with custom back implementation
@Override
public void onBackPressed(){
getActivity().getSupportFragmentManager().popBackStack();
}
We couldn’t find that file to show.
//https://developer.android.com/guide/navigation/navigation-pass-data#start
//Primary and detail nav graphs require data passed to them to initialize them with their tab number.
val primaryNavHostFragment =
childFragmentManager.findFragmentById(R.id.primary_nav_host_fragment) as NavHostFragment
var navController = primaryNavHostFragment.navController
navController.setGraph(
R.navigation.primary_nav_graph, bundleOf(
TAB_NUMBER to args.tabNumber
)
)
//https://developer.android.com/guide/navigation/navigation-pass-data#start
//Primary and detail nav graphs require data passed to them to initialize them with their tab number.
val primaryNavHostFragment =
childFragmentManager.findFragmentById(R.id.primary_nav_host_fragment) as NavHostFragment
var navController = primaryNavHostFragment.navController
navController.setGraph(
R.navigation.primary_nav_graph, bundleOf(
TAB_NUMBER to args.tabNumber
)
)
primaryViewModel.selection.observe(viewLifecycleOwner) {
val directions =
DetailNavGraphDirections.openDetail(args.tabNumber, "Selection observed: $it")
detailNavHostFragment.navController.navigate(directions)
binding.root.open()
}
binding.slidingPaneLayout.lockMode = SlidingPaneLayout.LOCK_MODE_LOCKED