u2gilles
u2gilles

Reputation: 7393

Jetpack compose navigation architecture without fragment?

I am a bit confused about the new Jetpack compose navigation component androidx.navigation:navigation-compose documented at https://developer.android.com/jetpack/compose/navigation.

Am I right to say that the single-activity architecture with 0 fragment is preferred to the single-activity architecture with multiple fragments when using Jetpack Compose ?

I know we can still use fragments and Jetpack compose in this manner :

class MyFragment: Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return ComposeView(requireContext()).apply{
            setContent {
                MyFragmentComposable()
            }
        }
    }
}

But I want to make sure that when using androidx.navigation:navigation-compose, we are not supposed to use fragment anymore, starting like so:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApp()
        }
    }
}

Upvotes: 22

Views: 6163

Answers (2)

harishanth raveendren
harishanth raveendren

Reputation: 614

The android developer website there are 2 possible ways to achieve the navigation and the recommendation is to first use fragments with compose and view based screens and then gradually get rid of fragments and have compose in place.

Upvotes: 0

Noah
Noah

Reputation: 3445

Yes, you are correct. Using no fragments is preferred. You can use a NavHost to declare your navigation graph.

Upvotes: 12

Related Questions