Moving from Activity to Fragment

advertisements

I have a problem with using Fragments within an Activity. I'd like to step into a Fragment from the Activity and I'm very close to do this. Here is my Activity:

Utils u;
Fragment fragment;
FrameLayout frameLayout;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn = (Button)findViewById(R.id.galleryID);
    frameLayout = (FrameLayout)findViewById(R.id.fragmentContainer);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {
                fragment = new GalleryFragment();
                Utils.fragmentManager(frameLayout.getId(), fragment, MainActivity.this);
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
    });

Utils:

public class Utils {
public static void fragmentManager(Integer contentId, Fragment fragment, Activity activity) {
    try {
        FragmentManager fm = activity.getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(contentId, fragment);
        fragmentTransaction.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

GalleryFragment:

 public class GalleryFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    // Defines the xml file for the fragment
    return inflater.inflate(R.layout.fragment_galleryfragment, parent, false);
}

// This event is triggered soon after onCreateView().
// Any view setup should occur here.  E.g., view lookups and attaching view listeners.
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // Setup any handles to view objects here
    // EditText etFoo = (EditText) view.findViewById(R.id.etFoo);
}

}

And my xmls activity_main:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res /android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:baselineAligned="false"
 android:orientation="horizontal" >

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:id="@+id/fragmentContainer"
    android:layout_centerHorizontal="true">

</FrameLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/galleryID"
    android:text="gallery"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>

And the fragment_galleryfragment.xml:

  <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/ok">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

 </LinearLayout>

When I click on the GALLERY button the layout of the gallery appears on the screen. But the activity_main layout stays where it is. I'd like to switch layout like switching from Activity A to Activity B. Can anyone help with this?


Try using

FragmentManager fm = activity.getSupportFragmentManager();
//instead of this
FragmentManager fm = activity.getFragmentManager();

rest of your code looks correct.

You should use

getSupportFragmentManager()

if you're using

android.support.v4.app.FragmentManager

and if you are using

android.app.FragmentManager

then you should use

getFragmentManager()