【问题标题】:Navigate to a particular fragment when app launched from sharesheet从共享表启动应用程序时导航到特定片段
【发布时间】:2019-11-06 12:27:54
【问题描述】:

我在我的应用程序中使用 Jetpack Navigation,它有一个 Activity 和两个片段(片段 A 也是主片段,片段 B 可以从片段 A 导航)。

MainActivity 中添加了一个intent-filter,如下图所示,可以接受纯文本。

 <activity android:name=".MainActivity"
        android:screenOrientation="portrait">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.SEND"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="@string/text_mime_type_text_plain"/>
        </intent-filter>
    </activity>

当用户从 Android 共享表中选择我的应用程序时,最终目标是将接收到的数据从 Intent 传递到 Fragment B。目前,我在 Fragment A(Home Fragment)中接收 Intent,然后使用 @987654324 @ 使用以下代码导航到片段 B。

  private fun checkReceivedIntent() {
    val receivedUrlIntent = activity?.intent
    val intentAction = receivedUrlIntent?.action
    val intentType = receivedUrlIntent?.type

    if (intentAction == Intent.ACTION_SEND && intentType != null) {
        if (intentType == getString(R.string.text_mime_type_text_plain) ) {
            val receivedText = receivedUrlIntent.getStringExtra(Intent.EXTRA_TEXT)
            val action = FragmentADirections.actionFragmentAToFragmentB(receivedText)
            findNavController().navigate(action)
        }
    }
}

问题是,当打开 Fragment B 时,按下工具栏的后退按钮或后退箭头(即使多次按下),Fragment A 没有出现,而 Fragment B 不断重新出现。

我不知道我错过了什么。当用户从另一个应用程序内的共享表打开应用程序时,这是打开特定片段的正确方法吗?

编辑

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">

<fragment
    android:id="@+id/navHostFragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />

<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bottomAppBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:fabCradleMargin="8dp"

    />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fabMainActivity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add"
    app:layout_anchor="@id/bottomAppBar" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

【问题讨论】:

  • 您能分享一下您是如何在 MainActivity xml 中定义容器的吗?另外我想知道你在哪里打电话给checkReceivedIntent?我假设它在 Fragment A 内?
  • @IbrahimAli 我已经更新了这个问题。是的,我在 Fragment A 中调用 checkReceivedIntent。
  • nav_graph.xml 中的&lt;action 中打开片段B 时是否使用popUp 或其他内容?
  • checkReceivedIntent() 将在您每次尝试加载 Fragment A 时被调用,因此即使您按下返回它也会继续导航到 B
  • 收到intents的值后,清空!因为当您导航回片段 A 时它仍然可用,它会再次获取值并继续前进...

标签: android android-fragments android-jetpack android-jetpack-navigation


【解决方案1】:

为什么你不能直接去Fragment B 收到Intent Extra 在你的Activity。只需而不是在 xml 中定义Fragment,或者作为startDestination,您只需执行切换到Fragment B

Activity

       Navigation
            .findNavController(binding.root)
            .navigate(
                R.id.navigation_from_intent_actions, bundle,
                NavOptions.Builder()
                    .setIntentExtra(extras)
                    .build()
            )

更新

要返回上一个片段,您可以轻松地从FragmentManager 获取Fragment(它不会被杀死,因为它是您的家Fragment)。您可以通过以下方式进行操作:

  private fun returnWithName() {
      val name = name_input.text.toString()

      (targetFragment as? FragmentA)?.setName(name)
      activity?.supportFragmentManager?
           .popBackStackImmediate()
  }

稍后只需检查来自FragmentB 的更新结果。您需要导航到哪个Fragment

注意。还有另一种与 Shared ViewModel 共享数据的方式。但是,您需要将结果从ViewModel 传输回FragmentA。然后检查新的交易参数。这是also described here

【讨论】:

  • 既然 Fragment A 已经被设置为 home 目的地,那么跳过它直接导航到 Fragment B 是不是一个好方法?
  • @MehulKanzariya 是的,这是另一回事。请检查更新的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 2022-11-28
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多