【问题标题】:Share web url from browser to navigation fragment从浏览器共享网址到导航片段
【发布时间】:2021-12-04 06:11:00
【问题描述】:

在我的应用中,我只有一个活动和 3 个片段(mainF、saveF、updateF)。

主要活动只设置布局activity_main,而这个活动布局只设置navHost Fragment。 我想将 url 从浏览器发送到片段之一,例如浏览器 -> 共享 url -> 片段 -> 保存。

以前这个应用程序有两个活动,所以我使用意图过滤器并获取 url 并存储在视图中。但是如何直接在nav graph的fragments中分享呢。

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    if (intent?.action == Intent.ACTION_SEND) {
        if ("text/plain" == intent.type) {
            intent.getStringExtra(Intent.EXTRA_TEXT)?.let {

                val navController = findNavController(R.id.navHostFragment)
                val bundle = Bundle()
                bundle.putString("urlFromWeb",it)
                navController.navigate(R.id.saveFragment,bundle)
            }
        }
    }
}

}

我的activity_main xml

<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewModel.MainActivity">

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/navHostFragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/main_navigation_file">


</androidx.fragment.app.FragmentContainerView>

【问题讨论】:

    标签: android kotlin android-intent


    【解决方案1】:

    您可以像以前一样在 Main Activity 中接收该数据,并打开所需的导航片段,绕过接收到的数据。

    【讨论】:

    • 我更新了问题。现在如何共享数据?
    • 使用 Safe Args 传递您的数据。
    • 请输入我无法理解。
    • 你可以在官方文档中找到最好的参考。 developer.android.com/guide/navigation/navigation-pass-data
    • 我在 saveFragment 中有名为“urlFromWeb”的字符串类型参数
    【解决方案2】:

    我们不能直接从我们的activity中访问nav contoller,所以使用了这个方法。

    主要活动

    class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    
        if (intent?.action == Intent.ACTION_SEND) {
            if ("text/plain" == intent.type) {
                intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
                   val navHost = supportFragmentManager.findFragmentById(R.id.navHostFragment) as NavHostFragment
                    val navContoller = navHost.findNavController()
                    val bundle = Bundle()
                    bundle.putString("url",it)
                    navContoller.navigate(R.id.saveFragment,bundle)
                }
            }
        }
    }
    

    }

    将使用意图的地方保存片段

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_save, container, false)
        // Getting the text that is sent from the MainActivity
        val message = arguments?.getString("url")
    
        titleEdit = view.findViewById(R.id.bookmark_title)
        urlEdit = view.findViewById(R.id.bookmark_url)
        descripEdit = view.findViewById(R.id.description_EditText)!!
        saveBtn = view.findViewById(R.id.savebtn)
        
        // Using that text in our view
        if (message != null) {
                urlEdit.setText(message)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多