Popbackstack Finish活动未弹出片段

时间:2019-07-17 09:04:30

标签: android android-fragments mvvm

我有一个活动,有两个片段。我正在替换OnCreate中的第一个片段,如下所示:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    profileFragment = Profile.newInstance()
    AppUtils.replaceFragment(profileFragment, supportFragmentManager, R.id.baseFragment)
}

点击按钮后,我将其称为:

AppUtils.addFragmentToStack(EditAddress.newInstance(), supportFragmentManager, R.id.baseFragment)

以上两种静态方法都是:

fun replaceFragment(fragment: android.support.v4.app.Fragment, fragmentManager: android.support.v4.app.FragmentManager, id: Int) {
        val transaction = fragmentManager.beginTransaction()
        transaction.replace(id, fragment)
        transaction.commit()
    }



fun addFragmentToStack(fragment: android.support.v4.app.Fragment, fragmentManager: android.support.v4.app.FragmentManager, id: Int) {
            val transaction = fragmentManager.beginTransaction()
            transaction.setCustomAnimations(R.anim.activity_out, R.anim.activity_in, R.anim.activity_in_exit, R.anim.activity_out_exit)
            transaction.replace(id, fragment)
            transaction.addToBackStack(null)
            transaction.commit()
        }

我尚未使用setContentView(),因为它已经在BaseActivity中。 我没有BaseActivity中的onbackPressed事件。

这是我的第一个片段:

class Profile : BaseFragment<ProfileViewModel, BaseListener>(R.layout.fragment_edit_own_address) {

    override fun getViewModelClass(): ProfileViewModel =
            ViewModelProviders.of(activity!!).get(ProfileViewModel::class.java)

    override fun getViewListener(): BaseListener? = activity as? BaseListener?

   override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        //Some methods here
   }

    companion object {
        fun newInstance() = EditAddressDialog().apply {
            arguments.apply {

            }
        }
    }
}

这是我的第二个片段:

 class EditAddress : BaseFragment<ProfileViewModel, BaseListener>(R.layout.fragment_edit_own_address) {

    override fun getViewModelClass(): ProfileViewModel =
            ViewModelProviders.of(activity!!).get(ProfileViewModel::class.java)

    override fun getViewListener(): BaseListener? = activity as? BaseListener?

    companion object {
        fun newInstance() = EditAddress().apply {
            arguments.apply {

            }
        }
    }
}

问题

当我开始活动profile片段显示正确性时,在单击按钮时我能够将EditAddress添加到Backstack,我已经在supportFragmentmanager.backStackEntry中对其进行了检查。

现在,当我按下“后退”按钮时,我无法弹出EditAddress片段。无需动画即可完成我的活动。当我从profile按后退按钮时,它以退出动画结束。

所以我认为FragmentManager中可能会发生一些崩溃,但是我找不到它。

注意

我尝试通过在活动中覆盖OnBackPressed()方法来实现,但是当我调用supportFragmentManager.popBackStack()时,它会完成Activity。

我没有在任何地方调用finish()方法。

0 个答案:

没有答案
相关问题