0

Here is my code

<fragment
  android:id="@+id/fragment1"
  android:name="com.example.app.Fragment1"
  android:label="SignatureFragment"
  tools:layout="@layout/layout_fragment1">
    <action
      android:id="@+id/action_fragment1_to_main_activity"
      app:destination="@id/main_activity"
      app:enterAnim="@anim/slide_in_from_right"
      app:exitAnim="@anim/no_anim"
      app:launchSingleTop="true"
      app:popEnterAnim="@anim/no_anim"
      app:popExitAnim="@anim/slide_out_to_right"
      app:popUpTo="@id/navigation_graph_id"
      app:popUpToInclusive="true" />
</fragment>

<activity
  android:id="@+id/main_activity"
  android:name="com.example.app.MainActivity"
  android:label="MainActivity"
  tools:layout="@layout/activity_main" />

Now the code for navigation

findNavController().navigate(R.id.action_fragment1_to_main_activity)

When I navigate to activity and press back, the fragment is still there. I want to clear the backstack after opening the activity.

I tried to remove the animation and also tried with removing app:launchSingleTop, but no success.

dakshbhatt21
  • 3,318
  • 3
  • 27
  • 37
  • Hi, what do you mean by navigating to activity; here there is a single activity and only navigation occurs in the level of fragments .. isn't you mean the home fragment of the `NavHostFragment`? – Zain Apr 28 '20 at 03:53
  • @Zain, actually I have few fragments before fragments, but they are removed from the backstack if I use `app:popUpTo="@id/navigation_graph_id"` and `app:popUpToInclusive="true"` – dakshbhatt21 Apr 28 '20 at 04:54
  • did you already check this question? https://stackoverflow.com/questions/50514758/how-to-clear-navigation-stack-after-navigating-to-another-fragment-in-android – G. Ciardini Apr 28 '20 at 13:23
  • @G.Ciardini: yes I checked that answer and already following the `app:popUpTo="@id/navigation_graph_id"` and `app:popUpToInclusive="true"` – dakshbhatt21 Apr 28 '20 at 13:39

1 Answers1

2

Edit Jetpack Navigation is intended to work with single activity and does not fully support activity navigation with parameters passed to actions

Thus to clear stack when navigating from one activity to another you will still need to call activity.finish()

Edit end

The thing is findNavController().navigate(R.id.action_fragment1_to_main_activity) wont work.

Try to navigate via navigate(@NonNull NavDirections directions). In your case it will look something like this

findNavController().navigate(
     Fragment1Directions.actionFragment1ToMainActivity())

Hope it helps.

Pavlo Ostasha
  • 8,161
  • 6
  • 23
  • I tried this but the same result. The fragment was there when I go back. – dakshbhatt21 Apr 28 '20 at 18:02
  • Hm weird. Are you sure you have you called 'finish()' inside an initial activity after navigating? – Pavlo Ostasha Apr 29 '20 at 06:58
  • do you mean the `finish()` while setting the graph from the very first activity here? `navController.setGraph(graph, extras)` it just closes the activity – dakshbhatt21 Apr 29 '20 at 16:24
  • 1
    Nope. I mean `finish()` after you call your method that navigates you to the other activity. Right after `findNavController().navigate( Fragment1Directions.actionFragment1ToMainActivity())`. It will do the thing. I've already added this explanation to the answer – Pavlo Ostasha Apr 29 '20 at 18:40