Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Fragment in Activity OnCreate

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

like image 753
Jason J Avatar asked Jul 10 '17 16:07

Jason J


People also ask

How do you load a fragment in an activity?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

Does fragment have onCreate?

onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.

How do I load a fragment in Kotlin?

Call the beginTransaction() method on the fragment manager instance. This returns a fragment transaction instance. 5. Call the add() method of the fragment transaction instance, passing through as arguments the resource ID of the view that is to contain the fragment and the fragment class instance.


2 Answers

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();
}
like image 135
Sagar Chapagain Avatar answered Nov 15 '22 08:11

Sagar Chapagain


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.

like image 27
Moria Yaroslav Avatar answered Nov 15 '22 07:11

Moria Yaroslav