Scoping States in Jetpack Compose

This is precisely what navigation graph scoped view models are used for. This involves two steps: Finding the NavBackStackEntry associated with the graph you want to scope the ViewModel to Pass that to viewModel(). For part 1), you have two options. If you know the route of the navigation graph (which, in general, you should), … Read more

How to change start destination of a navigation graph programmatically?

UPDATE: When you have nav graph like this: <fragment android:id=”@+id/firstFragment” android:name=”com.appname.package.FirstFragment” > <action android:id=”@+id/action_firstFragment_to_secondFragment” app:destination=”@id/secondFragment” /> </fragment> <fragment android:id=”@+id/secondFragment” android:name=”com.appname.package.SecondFragment”/> And you want to navigate to the second fragment and make it root of your graph, specify the next NavOptions: NavOptions navOptions = new NavOptions.Builder() .setPopUpTo(R.id.firstFragment, true) .build(); And use them for the navigation: Navigation.findNavController(view).navigate(R.id.action_firstFragment_to_secondFragment, … Read more

Make navigation drawer draw behind status bar

For API 21+ <style name=”AppTheme” parent=”android:Theme.Holo.NoActionBar.TranslucentDecor”> … </style> For API 19+ <style name=”AppTheme” parent=”Theme.AppCompat.Light.DarkActionBar”> <item name=”android:windowTranslucentStatus”>true</item> </style> Your layout should have android:fitsSystemWindows=”false” (which is the default). Now since you want to toggle the translucency you can do it programatically: Window window = getWindow(); // Enable status bar translucency (requires API 19) window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // Disable … Read more

Pass data back to previous fragment using Android Navigation

Android just released a solution for this; Passing data between Destinations (Navigation 2.3.0-alpha02), basically, in fragment A you observe changes in a variable and in fragment B you change that value before executing popBackStack(). Fragment A: findNavController().currentBackStackEntry?.savedStateHandle?.getLiveData<String>(“key”)?.observe(viewLifecycleOwner) { result -> // Do something with the result. } Fragment B: navController.previousBackStackEntry?.savedStateHandle?.set(“key”, result) navController.popBackStack()

Viewpager2 with fragments and Jetpack navigation: Restore fragments instead of recreating them

You can have initial pages of ViewPager as NavHostFragment which have their own back stacks which will result having the implementation in gif below Create a NavHost fragment for each tab or can have generalized one will add it either /** * Using [FragmentStateAdapter.registerFragmentTransactionCallback] with [FragmentStateAdapter] solves back navigation instead of using [OnBackPressedCallback.handleOnBackPressed] in every … Read more

FragmentContainerView using findNavController

As per this issue, when using FragmentContainerView, you need to find the NavController using findFragmentById() rather than using findNavController() when in onCreate(): val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment val navController = navHostFragment.navController This is because findNavController(R.id.nav_host_fragment) relies on the Fragment’s View to already be created which isn’t the case when using FragmentContainerView (as it uses … Read more

New navigation component from arch with nested navigation graph

The point is to get the right NavController to navigate in the right graph. Let’s take this scenario as an example: MainActivity |- MainNavHost |- NavBarFragment | |- NestedNavHost | | |-NestedContentFragment1 | | |-NestedContentFragment2 | | | |- BottomNavigationView | |- LoginFragment The main graph and the nested graph are in separate xml files: … Read more

Android Jetpack Navigation, BottomNavigationView with Youtube or Instagram like proper back navigation (fragment back stack)?

You don’t really need a ViewPager to work with BottomNavigation and the new Navigation architecture component. I have been working in a sample app that uses exactly the two, see here. The basic concept is this, you have the main activity that will host the BottomNavigationView and that is the Navigation host for your navigation … Read more

Setting launchMode=”singleTask” vs setting activity launchMode=”singleTop”

I think your definition of singleTop and singleTask is a little off. SingleTop could produce a duplicate instance. Lets use your example, League > Team > Position > Player. If there is a button in the player screen that will take you to the league screen, it will become League > Team > Position > … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)