是否可以使用Android导航架构组件(Android Jetpack)有条件地设置startDestination?

问题描述 投票:16回答:3

我正在使用NavigationAndroid Jetpack在屏幕之间导航。现在我想动态设置startDestination。

我有一个名为MainActivity的Activity和两个Fragments,FragmentA和FragmentB。

var isAllSetUp : Boolean = // It is dynamic and I’m getting this from Preferences.

    If(isAllSetUp)
    {
     // show FragmentA
    }
    else
    {
     //show FragmentB
    }

我想使用Navigation Architecture Component设置上面的流程。目前我使用startDestionation如下所示,但它不符合我的要求。

<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/lrf_navigation"
   app:startDestination="@id/fragmentA">

   <fragment
       android:id="@+id/fragmentA"
       android:name="com.mindinventory.FragmentA"
       android:label="fragment_a"
       tools:layout="@layout/fragment_a" />
</navigation>

是否可以使用Android导航架构组件有条件地设置startDestination?

android android-jetpack android-architecture-navigation
3个回答
23
投票

最后,我得到了一个解决方案...

将下面的代码放在onCreate()Activity方法中。

Kotlin代码

val navHostFragment = home_nav_fragment as NavHostFragment
val inflater = navHostFragment.navController.navInflater
val graph = inflater.inflate(R.navigation.nav_main)
graph.setDefaultArguments(intent.extras)
graph.startDestination = R.id.fragment1
//or
//graph.startDestination = R.id.fragment2

navHostFragment.navController.graph = graph

Java代码

NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.home_nav_fragment);  // Hostfragment
NavInflater inflater = navHostFragment.getNavController().getNavInflater();
NavGraph graph = inflater.inflate(R.navigation.nav_main);
graph.setDefaultArguments(getIntent().getExtras());
graph.setStartDestination(R.id.fragment1);

navHostFragment.getNavController().setGraph(graph);
navHostFragment.getNavController().getGraph().setDefaultArguments(getIntent().getExtras());


NavigationView navigationView = findViewById(R.id.navigationView);
NavigationUI.setupWithNavController(navigationView, navHostFragment.getNavController());

9
投票

Akash's answer以来,一些API已经更改,不可用或不是必需的。现在它有点简单了。

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  NavHostFragment navHost = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_main_nav_host);
  NavController navController = navHost.getNavController();

  NavInflater navInflater = navController.getNavInflater();
  NavGraph graph = navInflater.inflate(R.navigation.navigation_main);

  if (false) {
    graph.setStartDestination(R.id.oneFragment);
  } else {
    graph.setStartDestination(R.id.twoFragment);
  }

  navController.setGraph(graph);
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

  <!-- Following line omitted inside <fragment> -->
  <!-- app:navGraph="@navigation/navigation_main" -->
  <fragment
    android:id="@+id/fragment_main_nav_host"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="androidx.navigation.fragment.NavHostFragment"
  />

</androidx.constraintlayout.widget.ConstraintLayout>

navigation_main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Following line omitted inside <navigation>-->
<!-- app:startDestination="@id/oneFragment" -->
<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/navigation_main"
  >

  <fragment
    android:id="@+id/oneFragment"
    android:name="com.apponymous.apponymous.OneFragment"
    android:label="fragment_one"
    tools:layout="@layout/fragment_one"/>
  <fragment
    android:id="@+id/twoFragment"
    android:name="com.apponymous.apponymous.TwoFragment"
    android:label="fragment_two"
    tools:layout="@layout/fragment_two"/>
</navigation>

1
投票

这可以通过导航操作完成。因为fragmentA是您的起始目的地,所以在fragmentA中定义一个动作。

请注意以下两个字段:app:popUpToInclusive="true" app:popUpTo="@id/fragmentA"

<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/lrf_navigation"
   app:startDestination="@id/fragmentA">

   <fragment
       android:id="@+id/fragmentA"
       android:name="com.mindinventory.FragmentA"
       android:label="fragment_a"
       tools:layout="@layout/fragment_a">

    <action android:id="@+id/action_a_to_b"
            app:destination="@id/fragmentB"
            app:popUpToInclusive="true"
            app:popUpTo="@id/fragmentA"/>
   <fragment>

   <fragment
       android:id="@+id/fragmentB"
       android:name="com.mindinventory.FragmentB"
       android:label="fragment_b"
       tools:layout="@layout/fragment_b"/>
</navigation>

当您的MainActivity启动时,只需使用操作ID进行导航,它将删除堆栈中的fragmentA,并跳转到fragmentB。貌似,fragmentB是你的起始目的地。

if(!isAllSetUp)
{
  // FragmentB
  navController.navigate(R.id.action_a_to_b)
}
© www.soinside.com 2019 - 2024. All rights reserved.