Skip to content

Instantly share code, notes, and snippets.

View danielwilson1702's full-sized avatar

Daniel Wilson danielwilson1702

View GitHub Profile
<?xml version="1.0" encoding="utf-8"?>
<androidx.slidingpanelayout.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sliding_pane_layout">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/primary_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="@dimen/list_width"
class TwoPaneOnBackPressedCallback(
private val slidingPaneLayout: SlidingPaneLayout
) : OnBackPressedCallback(
// Set the default 'enabled' state to true only if it is slidable (i.e., the panes
// are overlapping) and open (i.e., the detail pane is visible).
slidingPaneLayout.isSlideable && slidingPaneLayout.isOpen
), SlidingPaneLayout.PanelSlideListener {
init {
slidingPaneLayout.addPanelSlideListener(this)
binding.slidingPaneLayout.lockMode = SlidingPaneLayout.LOCK_MODE_LOCKED
binding.slidingPaneLayout.lockMode = SlidingPaneLayout.LOCK_MODE_LOCKED
primaryViewModel.selection.observe(viewLifecycleOwner) {
val directions =
DetailNavGraphDirections.openDetail(args.tabNumber, "Selection observed: $it")
detailNavHostFragment.navController.navigate(directions)
binding.root.open()
}
//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
)
)
We couldn’t find that file to show.
@danielwilson1702
danielwilson1702 / FragmentWithBackImpl.java
Created September 5, 2015 12:17
Fragment with custom back implementation
@Override
public void onBackPressed(){
getActivity().getSupportFragmentManager().popBackStack();
}
@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)