片段管理器添加了错误的片段

时间:2019-04-11 20:24:09

标签: android android-fragments

为片段A中的一个按钮提供了一个onClickListener,以便转到片段A的onStart中的片段B:

override fun onStart() {


        enterMemberIDButton = view!!.findViewById(R.id.enter_member_id_button)
        enterPlateButton = view!!.findViewById(R.id.enter_plate_button)

        enterMemberIDButton.setOnClickListener {

            activity?.supportFragmentManager?.beginTransaction()
                ?.replace(R.id.main_view_pager, FragmentB.newInstance())?.addToBackStack(null)?.commit()
        }

}

但是,单击按钮会加载片段C,这已由我放置在片段C的onStart中的调试行确认。

这是片段A的xml:

<android.support.constraint.ConstraintLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <android.support.constraint.ConstraintLayout
            android:id="@+id/welcome_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" app:layout_constraintBottom_toTopOf="@+id/button_holder"
            android:layout_marginBottom="64dp" android:layout_marginTop="64dp"
            app:layout_constraintTop_toTopOf="parent">


        <TextView
                android:text="@string/welcome_member"
                android:textColor="@color/header_brown"
                android:textSize="36sp"
                android:textStyle="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/welcome_text" app:layout_constraintStart_toStartOf="parent"
                android:layout_marginLeft="8dp" android:layout_marginStart="8dp"
                app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                app:layout_constraintBottom_toTopOf="@id/please_select_text"/>
        <TextView

                android:text="@string/please_enter_member_id"
                android:textSize="32sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/please_select_text" android:layout_marginTop="8dp"
                app:layout_constraintTop_toBottomOf="@+id/welcome_container"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
                app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp" android:layout_marginBottom="8dp"
                app:layout_constraintBottom_toTopOf="parent" app:layout_constraintVertical_bias="0.2"/>
    </android.support.constraint.ConstraintLayout>
    <LinearLayout
            android:id="@+id/button_holder"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:weightSum="3" android:layout_marginTop="128dp"
            app:layout_constraintTop_toBottomOf="@+id/welcome_container" android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp"
            android:layout_marginStart="8dp" app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="8dp">
        <Button
                android:text="@string/enter_member_id"
                android:tag="15"
                android:background="@drawable/default_button_bg"
                android:textStyle="bold"
                android:textSize="36sp"
                android:padding="15dp"
                android:textAllCaps="false"
                android:textColor="@color/title_gray"
                android:layout_margin="16dp"
                android:layout_width="0dp"
                android:layout_height="200dp" android:id="@+id/enter_member_id_button" android:layout_weight="1"/>

        <Button
                android:text="@string/enter_plate"
                android:tag="14"
                android:background="@drawable/default_button_bg"
                android:textStyle="bold"
                android:textSize="36sp"
                android:padding="15dp"
                android:textAllCaps="false"
                android:layout_margin="16dp"
                android:textColor="@color/title_gray"
                android:layout_width="0dp"
                android:layout_height="200dp" android:id="@+id/enter_plate_button" android:layout_weight="1"/>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

和FragmentB(MemberIDFragment):

class MemberIDFragment : Fragment() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {


        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_member_id, container, false)
    }

    override fun onStart() {
        super.onStart()

        val editText: TextInputEditText = view!!.findViewById(R.id.member_edit_text)
        val keyboard: SoftKeyboard = view!!.findViewById(R.id.enter_id_keyboard)

        println("ID Fragment34")


        keyboard.init(context!!)


        val inputConnection = editText.onCreateInputConnection(EditorInfo())
        keyboard.inputConnection = inputConnection




    }


    companion object {

        @JvmStatic
        fun newInstance() =
            HaveCodeFragment().apply {

            }
    }
}

还有其他可能将其重新路由到错误的片段吗?

1 个答案:

答案 0 :(得分:1)

MemberIDFragment 正在返回HaveCodeFragment()的实例。

应将MemberIDFragment的newInstance()方法更改为:

companion object {
    @JvmStatic
    fun newInstance() = MemberIDFragment()
}