在活动OnCreate中加载片段[英] Load Fragment in Activity OnCreate

本文是小编为大家收集整理的关于在活动OnCreate中加载片段的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

问题:从片段1切换到片段2,开关发生,但两者都可见. (片段2开始焦点)

片段1是菜单范围 片段2是gamefragment

我相信这是因为我的片段1包含在我的主要活动中.xml

   <fragment
        android:id="@+id/menu_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment_menu">
    </fragment>

上面的代码xml将片段加载到主活动中.每当我使用此代码将菜单片段替换为菜单片段时:

public void replaceFragment(Fragment fragment) 
{
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.gameview_layout, gameFragment);
    fragmentTransaction.addToBackStack(fragment.toString());
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    fragmentTransaction.commit();
}

游戏片段出现,菜单片段上方叠加.如何使菜单片段消失?我敢肯定,简单的修复程序是可忽略的(false)或类似的内容,但我相信真正的问题是在XML文件中编程以首先包含菜单范围.

问题:如何在main.java类中编程以在创建上加载片段?

我想这样做,这样我就可以对其进行编程以加载片段1(菜单范围),而无需它已经存在.两者都可见的唯一原因是因为片段1包含在我的main.xml

推荐答案

代替创建两个片段,您应该创建一个单一的framelayout(或任何其他),并在开关期间替换框架布局,如下所示.

.

xml

<FrameLayout
    android:id="@+id/frameContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

kotlin

fun replaceFragment(fragment:Fragment){
    supportFragmentManager
            .beginTransaction()
            .replace(R.id.frameContainer, fragment)
            .commit()
}

java

public void replaceFragment(Fragment fragment) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.frameContainer, fragment);
    fragmentTransaction.addToBackStack(fragment.toString());
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    fragmentTransaction.commit();
}

其他推荐答案

尝试使用一些布局(例如线性延迟或framelayout)作为片段的容器,并在ongreate中制作

getFragmentManager().beginTransaction()
    .add(R.id.menu_fragment, MenuFragment.getInstance())
    .commit();

和重命名的容器ID为"@+id/fragmentContainer"或类似的内容.

其他推荐答案

在您的片段布局中

本文地址:https://www.itbaoku.cn/post/2091047.html

问题描述

Problem: Switching from fragment 1 to fragment 2, the switch happens, but both are visible. (fragment 2 comes into focus)

Fragment 1 is MenuFragment Fragment 2 is GameFragment

I believe this is happening because my fragment 1 is included in my main activity.xml

   <fragment
        android:id="@+id/menu_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment_menu">
    </fragment>

The code above in main.xml loads the fragment into the main activity. Whenever I replace the menu fragment with game fragment using this code:

public void replaceFragment(Fragment fragment) 
{
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.gameview_layout, gameFragment);
    fragmentTransaction.addToBackStack(fragment.toString());
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    fragmentTransaction.commit();
}

the game fragment appears and overlays above the menu fragment. How do I get the menu fragment to disappear? I'm sure an easy fix would be makeVisible(false) or something similar, but I believe the real problem is programming in the xml file to include the MenuFragment in the first place.

Question: How do I program in the main.java class to load a fragment on create?

I want to do this, so I can program it to load fragment 1 (MenuFragment) first without it already being there. The only reason both are visible is because fragment 1 is included in my main.xml

推荐答案

Instead of creating two fragments, you should create a single FrameLayout(or any other) and replace the frame layout during the switching as in example below.

XML

<FrameLayout
    android:id="@+id/frameContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Kotlin

fun replaceFragment(fragment:Fragment){
    supportFragmentManager
            .beginTransaction()
            .replace(R.id.frameContainer, fragment)
            .commit()
}

Java

public void replaceFragment(Fragment fragment) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.frameContainer, fragment);
    fragmentTransaction.addToBackStack(fragment.toString());
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    fragmentTransaction.commit();
}

其他推荐答案

Try to use some layout like LinearLayout or FrameLayout as a container for your fragments and in onCreate make something like

getFragmentManager().beginTransaction()
    .add(R.id.menu_fragment, MenuFragment.getInstance())
    .commit();

and rename container id to "@+id/fragmentContainer" or something similar.

其他推荐答案

In your fragment layout xml add a background color to solve the problem