[英]Is it possible to pass data from fragment to activity using Navigation Component ? If yes then please let me know how
[英]Pass data from single activity to start destination fragment using Architecture Component Navigation
我有以下Activity
,这是我的应用程序中的单个Activity
:
MainActivity.java :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
PermissionState state = PermissionState.get(this);
if (state == PermissionState.GRANTED) {
getWindow().setBackgroundDrawable(new ColorDrawable(-1));
}
super.onCreate(savedInstanceState);
ActivityMainBinding mainActivityBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
setSupportActionBar(mainActivityBinding.toolbarMain);
NavigationUI.setupWithNavController(mainActivityBinding.toolbarMain, Navigation.findNavController(this, R.id.nav_host_fragment));
}
}
与Activity
关联的布局如下,并且是根据此 Android Developers教程创建的。
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</LinearLayout>
</layout>
导航图如下:
nav_graph.xml :
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph"
app:startDestination="@id/mainFragment">
<fragment
android:id="@+id/mainFragment"
android:name="com.example.myapp.MainFragment"
android:label="@string/app_name"
tools:layout="@layout/fragment_main">
<action
android:id="@+id/action_mainFragment_to_settingsFragment"
app:destination="@+id/settingsFragment" />
</fragment>
<fragment
android:id="@+id/settingsFragment"
android:name="com.example.myapp.SettingsFragment"
android:label="@string/settings" />
</navigation>
现在,除了托管Activity
,我的应用程序有两个Fragment
,一个是主要的,另一个是设置。
主要Fragment
是我的应用程序的起始目的地,在那里我想检索MainActivity变量“PermissionState state”以根据权限的状态执行某些操作,比如隐藏或显示一些View
。
使用Android体系结构组件导航API版本1.0.0-beta01
以后,将数据从托管Activity
传递到起始目的地Fragment
的正确,非弃用策略是什么,如果可能,可以使用Safe Args ?
找到一个解决方案,工作正常,但我不喜欢它必须有一个更好的解决方案。
在您的活动中:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navController = findNavController(R.id.nav_controller_fragment)
val bundle = Bundle()
bundle.putString("name","your value")
navController.setGraph(navController.graph,bundle)
}
在你的开始片段:
val args: MainFragmentArgs by navArgs()
textView.text = args.name
在你的navGraph中:
<navigation 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:id="@+id/mobile_navigation"
app:startDestination="@id/mainFragment">
<fragment android:id="@+id/mainFragment"
android:name="com.haidar.mediasaver.fragments.MainFragment"
android:label="main_fragment"
tools:layout="@layout/main_fragment">
<argument android:name="name"
app:argType="string"/>
</fragment>
如果您使用安全args,则需要执行以下操作:
app:navGraph
从属性NavHostFragment
然后打电话:
findNavController(R.id.nav_controller_fragment) .setGraph( R.navigation.nav_graph, MainFragmentArgs("your values").toBundle() )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.