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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…