How TO Change Activity To Fragment

前端 未结 2 841
予麋鹿
予麋鹿 2020-12-17 05:32

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?

2条回答
  •  轮回少年
    2020-12-17 06:13

    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

提交回复
热议问题