Load Fragment in Activity OnCreate

早过忘川 提交于 2019-12-07 12:58:35

问题


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


回答1:


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();
}



回答2:


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.




回答3:


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




回答4:


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.

JAVA

public void loadFragment(Fragment fragment) {
    String backStateName = fragment.getClass().getName();
    FragmentManager fragmentManager = getSupportFragmentManager();
    boolean fragmentPopped = fragmentManager.popBackStackImmediate(backStateName, 0);
    if (!fragmentPopped) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frameContainer, fragment);
        fragmentTransaction.addToBackStack(backStateName);
        fragmentTransaction.commit();
    }
}

XML

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


来源:https://stackoverflow.com/questions/45017757/load-fragment-in-activity-oncreate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!