我有以下Activity
,这是单一个在我的应用程序:
main activity.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
相关联的布局是以下和创建以下this Android开发教程。
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>
该navigation graph如下:
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
s,一个是主要的,另一种是设置。
主要Fragment
是我的应用程序的起始目标,还有我想找回在MainActivity变量“PermissionState状态”进行基于权限的状态的一些操作,如隐藏或显示一些View
s。
采用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>
如果您使用的安全指定参数时,你需要做的:
app:navGraph
删除NavHostFragment
属性findNavController(R.id.nav_controller_fragment)
.setGraph(
R.navigation.nav_graph,
MainFragmentArgs("your values").toBundle()
)