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

How to pass object of type Parcelable to a Fragment using Navigation type safeargs plugin

Since safe-args-gradle-plugin:1.0.0-alpha03 you can use Parcelable objects by using their fully qualified class name: <argument android:name=”item” app:argType=”com.example.app.model.Item”/> Parcelable arguments are now supported, using a fully qualified class name for app:type. The only default value supported is “@null” (https://issuetracker.google.com/issues/79563966) Source: https://developer.android.com/jetpack/docs/release-notes To support nullability one has to use android:defaultValue=”@null” with app:nullable=”true”.

android:autoSizeTextType in Jetpack Compose

I use the following to adjust the font size with respect to the available width: val textStyleBody1 = MaterialTheme.typography.body1 var textStyle by remember { mutableStateOf(textStyleBody1) } var readyToDraw by remember { mutableStateOf(false) } Text( text = “long text goes here”, style = textStyle, maxLines = 1, softWrap = false, modifier = modifier.drawWithContent { if (readyToDraw) … Read more

What is the use of androidx.legacy:legacy-support-v4: dependency

androidx.legacy:legacy-support-v4 is Androidx artifacts of com.android.support:support-v4 com.android.support:support-v13 -> androidx.legacy:legacy-support-v13 com.android.support:support-v4 -> androidx.legacy:legacy-support-v4 You can find info about the library mapping here The Support Library is a static library that you can add to your Android application in order to use APIs that are either not available for older platform versions or utility APIs that aren’t … 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()

Jetpack Compose: Launch ActivityResultContract request from Composable function

As of androidx.activity:activity-compose:1.3.0-alpha06, the registerForActivityResult() API has been renamed to rememberLauncherForActivityResult() to better indicate the returned ActivityResultLauncher is a managed object that is remembered on your behalf. val result = remember { mutableStateOf<Bitmap?>(null) } val launcher = rememberLauncherForActivityResult(ActivityResultContracts.TakePicturePreview()) { result.value = it } Button(onClick = { launcher.launch() }) { Text(text = “Take a picture”) } … Read more

How to draw a circular image in Android Jetpack Compose?

There’s a clip modifier which can be applied to any composable as well as the Image, just pass a CircleShape into it: Image( painter = painterResource(R.drawable.sample_avatar), contentDescription = “avatar”, contentScale = ContentScale.Crop, // crop the image if it’s not a square modifier = Modifier .size(64.dp) .clip(CircleShape) // clip to the circle shape .border(2.dp, Color.Gray, CircleShape) … Read more

Proguard causing runtime exception with Android Navigation Component

I know that Proguard and R8 should be keeping all the children of library classes but in this case, the fragment class seems to be missing. This keep rule solved my issue but technically we should not need this rule at all! -keep class * extends android.support.v4.app.Fragment{} If you are using AndroidX, then use this … Read more