【问题标题】:Open fragment from activity with Jetpack Navigation使用 Jetpack Navigation 从活动中打开片段
【发布时间】:2021-03-02 19:28:35
【问题描述】:

我真的在努力寻找一种方法来使用导航控制器从活动中打开片段。

我收到一条错误消息,指出作为按钮的视图不包含导航控制器。

我尝试使用按钮视图创建导航控制器,但这会导致错误提示它不包含导航控制器

我想从按钮的 onclick 监听器开始片段:

register_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent registerIntent = new Intent(StartActivity.this, RegisterActivity.class);
            startActivity(registerIntent);
            finish();
        }
    });
}

布局代码

<?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=".LoginRegister.StartActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to Aston Connect"
        android:textColor="@color/black"
        android:textSize="24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.38" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:text="Do you have an account?"
        android:textColor="@color/black"
        android:textSize="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.13" />

    <Button
        android:id="@+id/register_btn"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Need a new account?"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:layout_constraintVertical_bias="0.365" />

    <Button
        android:id="@+id/login_btn"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Already have an account?"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/register_btn"
        app:layout_constraintVertical_bias="0.17000002" />


</androidx.constraintlayout.widget.ConstraintLayout>

我的导航图:

<?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/navgraph"
    app:startDestination="@id/newsfeedFragment">

    <fragment
        android:id="@+id/activityOverviewFragment"
        android:name="com.devin.astonconnect.ActivityOverviewFragment"
        android:label="fragment_activity_overview"
        tools:layout="@layout/fragment_activity_overview" >
        <action
            android:id="@+id/action_activityOverviewFragment_to_selectedPostFragment"
            app:destination="@id/selectedPostFragment" />
        <action
            android:id="@+id/action_activityOverviewFragment_to_profileFragment"
            app:destination="@id/profileFragment" />
    </fragment>


    <fragment
        android:id="@+id/newsfeedFragment"
        android:name="com.devin.astonconnect.NewsfeedFragment"
        android:label="fragment_newsfeed"
        tools:layout="@layout/fragment_newsfeed">
        <action
            android:id="@+id/action_homeFragment_to_profileFragment"
            app:destination="@id/profileFragment" />
    </fragment>
    <fragment
        android:id="@+id/chatFragment"
        android:name="com.devin.astonconnect.Chat.ChatFragment"
        android:label="fragment_chat"
        tools:layout="@layout/fragment_chat" />
    <fragment
        android:id="@+id/searchFragment"
        android:name="com.devin.astonconnect.SearchFragment"
        android:label="fragment_search"
        tools:layout="@layout/fragment_search" >
        <action
            android:id="@+id/action_searchFragment_to_profileFragment"
            app:destination="@id/profileFragment" />
    </fragment>
    <fragment
        android:id="@+id/journalFragment"
        android:name="com.devin.astonconnect.Journal.JournalFragment"
        android:label="fragment_search"
        tools:layout="@layout/fragment_journal" >
        <action
            android:id="@+id/action_journalFragment_to_journalEntry1Fragment"
            app:destination="@id/journalEntry1Fragment" />
        <action
            android:id="@+id/action_journalFragment_to_viewJournalEntryFragment"
            app:destination="@id/viewJournalEntryFragment" />
    </fragment>
    <fragment
        android:id="@+id/profileFragment"
        android:name="com.devin.astonconnect.ProfileFragment"
        android:label="fragment_profile"
        tools:layout="@layout/fragment_profile" >
        <action
            android:id="@+id/action_profileFragment_to_selectedPostFragment"
            app:destination="@id/selectedPostFragment" />
    </fragment>
    <fragment
        android:id="@+id/selectedPostFragment"
        android:name="com.devin.astonconnect.SelectedPostFragment"
        android:label="fragment_selected_post"
        tools:layout="@layout/fragment_selected_post" />
    <fragment
        android:id="@+id/journalEntry1Fragment"
        android:name="com.devin.astonconnect.Journal.JournalEntry1Fragment"
        android:label="fragment_journal_entry1"
        tools:layout="@layout/fragment_journal_entry1" >
        <action
            android:id="@+id/action_journalEntry1Fragment_to_journalEntry2Fragment"
            app:destination="@id/journalEntry2Fragment" />
    </fragment>
    <fragment
        android:id="@+id/journalEntry2Fragment"
        android:name="com.devin.astonconnect.Journal.JournalEntry2Fragment"
        android:label="fragment_journal_entry2"
        tools:layout="@layout/fragment_journal_entry2" >
        <argument
            android:name="journalItemArg"
            app:argType="com.devin.astonconnect.Model.JournalItem" />
        <action
            android:id="@+id/action_journalEntry2Fragment_to_newsfeedFragment"
            app:destination="@id/newsfeedFragment" />
    </fragment>
    <fragment
        android:id="@+id/addReflectionFragment"
        android:name="com.devin.astonconnect.Journal.addReflectionFragment"
        android:label="fragment_journal_entry4"
        tools:layout="@layout/fragment_add_reflection" >
        <action
            android:id="@+id/action_addReflectionFragment_to_newsfeedFragment"
            app:destination="@id/newsfeedFragment" />
    </fragment>
    <fragment
        android:id="@+id/viewJournalEntryFragment"
        android:name="com.devin.astonconnect.Journal.ViewJournalEntryFragment"
        android:label="fragment_view_journal_entry"
        tools:layout="@layout/fragment_view_journal_entry" >
        <action
            android:id="@+id/action_viewJournalEntryFragment_to_addReflectionFragment"
            app:destination="@id/addReflectionFragment" />
    </fragment>
</navigation>

【问题讨论】:

  • 活动布局中的按钮?请包括您的代码和布局。
  • nav_graph.xml 宁愿是相关的,因为代码与标题不匹配 - 也不与描述匹配;这是Activity 而不是Fragment

标签: android android-jetpack android-architecture-navigation android-jetpack-navigation


【解决方案1】:

我通过创建一个单独的导航图,将我需要的片段添加到它,并将其用于导航来解决这个问题。我不想使用我在整个应用中一直使用的主导航图,因为理想情况下应该只有一个与之关联的活动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    相关资源
    最近更新 更多