0

I have a simple application consisting of one MainActivity and two fragments, FragmentA and FragmentB. I want to use a NavigationGraph such that users still land on MainActivity but then can use two buttons to navigate to either FragmentA or FragmentB. Is there a way to do that? Based on the stuff on DAC, it looks like I have to use three fragments plus the MainActivity. But I was wondering if I can just use my existing scheme of 1 Activity and 2 Fragments.

Adding details to avoid confusion?

MainActivity has two buttons: User clicks on "Do A" to go to FragmentA and on "Do B" to go to FragmentB.

However, to use the Navigation Graph, it looks like I have to add one more fragment -- MainFragment. My question is: is there a way to avoid having to add an additional fragment and still use Navigation Graph.

salyela
  • 1,325
  • 3
  • 14
  • 26
  • 2
    The only thing the `MainActivity` should be doing is adding a `NavHostFragment` that actually hosts the contents of each screen in your app. What is your MainActivity showing before the user goes to FragmentA or FragmentB? Shouldn't that be its own fragment (i.e., the starting destination of your graph)? – ianhanniballake Oct 06 '21 at 18:02
  • That is my question indeed: must I add a third fragment or is there a way to avoid adding that third fragment and still use Navigation Graph. – salyela Oct 07 '21 at 13:56

1 Answers1

1

Android applications need at least one activity to show UI. A fragment is just like a button in an android app. You cant show it without attaching it to an activity. In the android navigation tutorial, there's one activity. That activity only contains tag so it is actually empty. In fragment tag, you show a fragment and that tutorial show you how to switch fragment inside tag. So you are always inside MainActivity even when your app show a different UI(switching between fragment).

just like ianhanniballake comment above.. What does your activity show? What do you expect it to show? you always see it.

Edit: Navigation graph only works for fragments. If you want to navigate between activities, use this Code:

Intent switchActivityIntent = new Intent(this, TargetActivity.class);
startActivity(switchActivityIntent);

You can use navigation graph like usual and when you need to open activity just use that code. It works fine.. But why do u need to use more than one activity when u using navigation graph? you should only have one activity when using navigation graph. That is why google make that thing(navigation graph) to support Single-Activity architecture.

  • I thought the question was clear, but just in case, I've edited to show more details. – salyela Oct 07 '21 at 13:54
  • Wait.. so you want to add fragment and you don't know how so you make activity instead? Dude just add your fragment to navigation graph. It can contain more than just two fragment. See this official tutorial It contains 6 fragment: https://developer.android.com/guide/navigation/navigation-getting-started. – Bobby Anggunawan Oct 07 '21 at 19:31