Monday, 23 June 2014

[KEY : ANDROID FRAGMENT, ACTIVITY, ADD FRAGMENT TO ACTIVITY]

Add Fragment to Activity

  1. Create simple activity with xml.
  2. Go to xml file add framelayout and assign id to it as follow:

     <FrameLayout
            android:id="@+id/your_frame_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true" />

  3. Create java class with name like "MyFirstFragment.java" and extend fragment.
  4. Go to activity add the following code :

    Fragment myFragment = new MyFirstFragment();
     FragmentManager fragmentManager = getSupportFragmentManager();
     FragmentTransaction fTransaction = fragmentManager
       .beginTransaction()
       .replace(R.id.your_frame_layout, bodyFragment)
       .addToBackStack("myprofile");
       
     fTransaction.commit();
       


No comments:

Post a Comment