【发布时间】: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