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:
- Create your main (activity layout) file. In it, add a frame layout which will act as a container for your fragments.
- Now create your two fragments. This involves creating two xml files that will be inflated inside your fragment's onCreateView method.
- 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.
- Now create an interface inside your first fragment and add a method in it that your activity should override after implementing the interface.
- When the user clicks that button, inside onClick method, you should call the interface method to notify the activity of the click event.
- 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.
- Remember to check if your fragment exists first before simply adding one to view.
I hope these steps help you.
Sample Code
publicclassWizardActivityextendsActivityimplementsSecondFragment.OnButtonClickedListener
{
private FirstFragment firstFragment;
publicvoidonCreate(Bundle saveInstanceState)
{
super.onCreate(saveInstanceState);
setContentView(R.layout.main);
firstFragment = newFirstFragment();
setFragment(firstFragment, "firstFragment");
}
@OverridepublicvoidloadSecondFragment()
{
SecondFragmentsecondFragment=newSecondFragment();
setFragment(secondFragment, "secondFragment");
}
publicvoidsetFragment(Fragment frag, String tag)
{
FragmentManagerfm= getFragmentManager();
FragmentTransactionft= fm.beginTransaction();
Fragmentfragment= 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--><FrameLayoutandroid: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
publicclassFirstFragmentextendsFragmentimplementsView.OnClickListener
{
private Activity mActivity;
@OverridepublicvoidonAttach(Activity act)
{
super.onAttach(act);
this.mActivity = act;
/*Initialize whatever you need here*/
}
@Overridepublic View onCreateView(LayoutInflator inflator, ViewGroup container, Bundle saveInstanceState)
{
Viewv= inflator.inflate(R.layout.first_fragment, container, false);
Buttonbutton= (Button)v.findViewById(R.id.button);
button.setOnClickListener(this);
}
@OverridepublicvoidonClick(View v)
{
((OnButtonClickListener), mActivity).loadSecondFragment();
}
publicinterfaceOnButtonClickListener
{
voidloadSecondFragment();
}
}
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"