android - 使用导航组件时不设置 Fragment 的 id 和 tag

标签 android android-fragments

我的问题是我的 fragment 的 id 和标签 虽然我在我的 mobile_navigation.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/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() 以编程方式访问我的 fragment 中的 id 和标签时或 this.getTag() id 始终相同 非常高的整数 对于所有 fragment (比 max int 小一点),标签是 .
导航放在我的主要 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 找到演示意外行为的说明。 .

最佳答案

fragment 标签 进入 导航标签 它是一个目的地(Anatomy of a destination here),因此您可以定义:id、name、label。

<navigation>
    <fragment></fragment>
</navigation>
不像 fragment 标签 进入 xml 布局 ,其中实现得到 安卓:标签属性并在从 膨胀 fragment 时使用它机器人:名称 .
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   <fragment
      android:id="@+id/someFragment"
      android:tag="someTagNameToSomeFragment"
      android:name="com.somepackagename.SomeFragment"/>
</LinearLayout>
也是如此。安卓:id ,它是该目的地的 id 资源,而不是 fragment ID .我们有两种方法可以为 Fragment 设置 id,或者从 xml 布局文件(如上面的示例)或通过事务。从事务(从 Activity 提交 kotlin 扩展):
supportFragmentManager.commit { add(R.id.someFragment, SomeFragment()) }
我假设 R.id.nav_host_fragment 是您提供给在 xml 布局上定义的 NavHostFragment 容器( View )的 id。在这种情况下,每次导航到新的 目的地之前的 Fragment View 被销毁,新的 Fragment 及其 View 被创建。这些情况下的 Fragment id 将是 R.id.nav_host_fragment (在 xml 布局上定义的 NavHostFragment 容器( View )ID)。

关于android - 使用导航组件时不设置 Fragment 的 id 和 tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62297866/

相关文章:

android - 带有操作栏的即时 "duplicate" fragment

Android:Singleton DefaultHttpClient 不会保持 session

Android微调器在旋转时表现得很奇怪

android - 如何在 Android kotlin 上的 fragment 中初始化上下文

android - 如何将数据从当前 fragment 传递到前一个 fragment

android在fragment上实现寻呼机时出错

无需滑动的Android选项卡布局

android - 单击网络应用程序中的按钮会显示按钮后面的输入,然后无意中关注输入

java - android studio 中的按钮背景颜色始终为紫色

Android - 如何以编程方式点击 ListView 项目