Skip to content Skip to sidebar Skip to footer

Can't Add A Fragment When Creating A New Activity

i'm trying to create a wizard Like Android application, i want to Create an activity and Two dynamic Fragments, the first one will be added when the Activity is created, and the s

Solution 1:

Follow these steps:

  1. Create your main (activity layout) file. In it, add a frame layout which will act as a container for your fragments.
  2. Now create your two fragments. This involves creating two xml files that will be inflated inside your fragment's onCreateView method.
  3. One of your fragments (the first one) should have a button that the user will be able to click. That means you must attach an onClick listener to it inside the onCreateView method after finding it by id.
  4. Now create an interface inside your first fragment and add a method in it that your activity should override after implementing the interface.
  5. When the user clicks that button, inside onClick method, you should call the interface method to notify the activity of the click event.
  6. Inside the activity, when the method is called, create a new instance of the second fragment and add it to view by replacing the first one - or it depends on whether you are using two-pane layout in your activity - in that case, you just add the fragment.
  7. Remember to check if your fragment exists first before simply adding one to view.

I hope these steps help you.

Sample Code

public class WizardActivity extends Activity implements SecondFragment.OnButtonClickedListener
{
   private FirstFragment firstFragment;

   public void onCreate(Bundle saveInstanceState)
   {
       super.onCreate(saveInstanceState);

       setContentView(R.layout.main);

       firstFragment = new FirstFragment();

       setFragment(firstFragment, "firstFragment");
   }


   @Override
   public void loadSecondFragment()
   {
       SecondFragment secondFragment = new SecondFragment();

       setFragment(secondFragment, "secondFragment");
   }

   public void setFragment(Fragment frag, String tag)
   {
       FragmentManager fm = getFragmentManager();
       FragmentTransaction ft = fm.beginTransaction();

       Fragment fragment = getFragmentManager().findFragmentById(R.id.fragmentContainer);
       if(fragment == null)
       {
           ft.add(R.id.fragmentContainer, frag, tag);
       } else {
          ft.replace(R.id.fragmentContainer, frag, tag);
       }

       ft.addToBackStack(null);
       ft.commit()

   }

}

Now the xml file for main layout.

<LinearLayout ........>

    <!--add whatever you need here-->

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

</LinearLayout>

Now let us create one of your fragments - the first one:

FirstFragment.java

public class FirstFragment extends Fragment implements View.OnClickListener
{
    private Activity mActivity;

    @Override
    public void onAttach(Activity act)
    {
        super.onAttach(act);
        this.mActivity = act;

        /*Initialize whatever you need here*/
    }

    @Override
    public View onCreateView(LayoutInflator inflator, ViewGroup container, Bundle saveInstanceState)

    {
        View v = inflator.inflate(R.layout.first_fragment, container, false);

        Button button = (Button)v.findViewById(R.id.button);
        button.setOnClickListener(this);
    }


    @Override
    public void onClick(View v)
    {
        ((OnButtonClickListener), mActivity).loadSecondFragment();
    }

    public interface OnButtonClickListener
    {
        void loadSecondFragment();
    }
}

You should be able to just create the second fragment and have it loaded in the activity when a button is clicked.

Good luck.


Post a Comment for "Can't Add A Fragment When Creating A New Activity"