android-fragments 使用导航组件时未设置片段的ID和标记

qgzx9mmu  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(95)

我遇到了这样的问题:虽然我在mobile_navigation.xml中指定了片段的id和tag,但没有设置它们

<?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/mobile_navigation"
    app:startDestination="@+id/nav_frag1">

    <fragment
        android:id="@+id/nav_frag1"
        android:tag="abcdef"
        android:name="org.example.myproject.MyFragment"
        android:label="@string/menu_frag1"
        tools:layout="@layout/fragment_frag1" />

    ...

</navigation>

当我尝试通过this.getId()this.getTag()以编程方式访问片段中的id和标记时,id对于所有片段总是相同的非常大的整数(比max int小一点),标记是null
导航放在我的主Activity中。代码基本上是Android Studio创建的示例抽屉导航项目。我没有对它做任何更改。

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.frag1, R.id.frag2, R.id.frag3)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

}

我是否遗漏了一些关于id和tag必须设置的方式的信息?
编辑:可以在https://github.com/flauschtrud/FragmentTagAndIdTest中找到一个示例项目,它演示了意外行为。

kpbpu008

kpbpu0081#

fragment标记转换为navigation标记它是一个destination(这里解剖一个destination),因此可以定义:ID、名称、标签。

<navigation>
    <fragment></fragment>
</navigation>

fragment标记不同,在xml布局中,实现获取android:tag attr,并在从android:name扩展片段时使用它。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   <fragment
      android:id="@+id/someFragment"
      android:tag="someTagNameToSomeFragment"
      android:name="com.somepackagename.SomeFragment"/>
</LinearLayout>

同样的情况也发生在android:id上,它是该目的地的id资源,而不是Fragment id。我们有两种方法为Fragment设置id,一种是从xml布局文件(如上面的例子),另一种是通过事务。从事务(从Activity提交Kotlin扩展):

supportFragmentManager.commit { add(R.id.someFragment, SomeFragment()) }

我假设R.id.nav_host_fragment是您为NavHostFragment容器给予的id(视图)。在这种情况下,每次导航到新的目标时,在销毁片段视图并创建新的片段及其视图之前。在这些情况下,片段ID将为R.id.nav_host_fragment(在xml布局上定义的NavHostFragment容器(视图)ID)。

相关问题