简体   繁体   English

从片段导航到活动时,后台堆栈不清除

[英]backstack does not clear while navigating from fragment to activity

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.当我导航到活动并按回时, fragment仍然存在。 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.我尝试删除 animation 并尝试删除app:launchSingleTop ,但没有成功。

Edit Jetpack Navigation is intended to work with single activity and does not fully support activity navigation with parameters passed to action s编辑Jetpack Navigation 旨在与单个活动一起使用,并且不完全支持将参数传递给action的活动导航

Thus to clear stack when navigating from one activity to another you will still need to call activity.finish()因此,要在从一个活动导航到另一个活动时清除堆栈,您仍然需要调用activity.finish()

Edit end编辑结束

The thing is findNavController().navigate(R.id.action_fragment1_to_main_activity) wont work.问题是findNavController().navigate(R.id.action_fragment1_to_main_activity)不起作用。

Try to navigate via navigate(@NonNull NavDirections directions) .尝试通过navigate(@NonNull NavDirections directions) In your case it will look something like this在你的情况下,它看起来像这样

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

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM