Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 动态替换片段?_Android_Android Fragments - Fatal编程技术网

Android 动态替换片段?

Android 动态替换片段?,android,android-fragments,Android,Android Fragments,现在我知道这是一个非常简单的问题,并且被问了很多次,但之前提供的解决方案中没有一个对我有效 我有一个框架布局如下的活动: <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://sc

现在我知道这是一个非常简单的问题,并且被问了很多次,但之前提供的解决方案中没有一个对我有效

我有一个框架布局如下的活动:

<android.support.constraint.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=".MainActivity">

    <FrameLayout
        android:id="@+id/replace_me"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </FrameLayout>

</android.support.constraint.ConstraintLayout>

第一个片段显示正确。但是,当用户单击菜单中的某个项目并登录时,应用程序应将当前片段替换为新片段

private void showAuthenticationDialog() {
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

    Fragment fragmentByTag = getSupportFragmentManager().findFragmentByTag(AuthenticationDialogFragment.TAG);

    if (fragmentByTag != null) {
        fragmentTransaction.remove(fragmentByTag);
    }
    fragmentTransaction.addToBackStack(null);

    AuthenticationDialogFragment authenticationDialogFragment = AuthenticationDialogFragment.newInstance(() -> {
        replaceFragment(new ParentControlPanelFragment(), true);
    });
    authenticationDialogFragment.show(fragmentTransaction, AuthenticationDialogFragment.TAG);

}

即使在提交之后,新片段也不会被替换。我可以看到我的日志消息,但看不到新的片段。我不太确定问题出在哪里。

问题是我试图在关闭对话框之前替换片段


正确的方法是先关闭对话框,然后尝试替换片段。

“应用程序应该用新片段替换当前片段”--不,您的代码显示的是一个对话框。它与之前的
FragmentTransaction
@commonware无关显示“身份验证”对话框并单击登录后,应显示“ParentControlPanelFragment”,而不是casehmm。。。尝试暂时删除
AuthenticationDialogFragment
及其
show()
。尝试直接调用
showAuthenticationDialog()
call
replaceFragment(newparentControlPanelFragment(),true)
。如果这是可行的,那么我最好的猜测是关于在哪里使用lambda表达式的问题。如果这不起作用,那么可能是
ParentControlPanelFragment
有问题(例如,缺少
onCreateView()
)。@commonware我尝试按照您所说的删除AuthenticationDialog,结果成功了。现在我可以看到碎片了。但是,如果lambda正确调用replace方法,那么它可能会出现什么问题呢。即使在显示身份验证对话框时,我也可以看到记录的消息您当前的结构可能有重叠的事务:在涉及
AuthenticationDialogFragment
的事务完成之前,您尝试替换
R.id.replace\u me
片段。也许取消对话框基本上会同时删除两个事务:对话框的事务和您在对话框内尝试运行的事务。
private void showAuthenticationDialog() {
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

    Fragment fragmentByTag = getSupportFragmentManager().findFragmentByTag(AuthenticationDialogFragment.TAG);

    if (fragmentByTag != null) {
        fragmentTransaction.remove(fragmentByTag);
    }
    fragmentTransaction.addToBackStack(null);

    AuthenticationDialogFragment authenticationDialogFragment = AuthenticationDialogFragment.newInstance(() -> {
        replaceFragment(new ParentControlPanelFragment(), true);
    });
    authenticationDialogFragment.show(fragmentTransaction, AuthenticationDialogFragment.TAG);

}