I want To change my Activity
to Fragment
I tried hard but I am unable to Change my Activity To Fragment. Can Anyone Please Tell me How to do this?
Changing an Activity class to Fragment class requires extending Fragment super class instead of Activity , in addition to implementing some Callback methods that are specific to fragments, like: OnCreateView(), OnActivityCreated() ...
Fragments can be started and initiated either programmatically by another fragments, activities or using fragment tag in the XML layout from within another fragment or activity.
Please check the fragment guide on android developers site:
http://developer.android.com/training/basics/fragments/creating.html
OR
http://developer.android.com/guide/components/fragments.html
Just understand some steps then you can easily convert a Activity to Fragment now and also in future..:
First of all instead of extending Activity
,just extend Fragment
..
Ex: public class Welcome extends Fragment{
Then override onCreateView()
..
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
.....
}
Then inflate the layout through LayoutInflater and asign to a View
for further use in subview inilialization..
like: View mView = inflater.inflate(R.layout.welcome, null);
Then initialize all sub views with the help of main view..like:
ImageView image = (ImageView) mView.findViewById(R.id.imageView1);
TextView userfullname = (TextView) mView.findViewById(R.id.userfullname);
Now do all your tasks same like activity here..
The important thing.. Use getActivity()
in the place of context
..
Ex: Toast.maketext(getActivity(), "...", Toast.LENGTH_LONG).show();
For more information ,just visit Fragment in developers block..
Thank you