Convert Activity to Fragment Step by Step

public class Main extends Activity {
 
private TextView mTextView;
private Activity mAct;
private Intent mIntent;
 
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main_layout);
  mTextView = findViewById(R.id.mTextView);
  mAct = getActivity();
  mIntent = getIntent();
 }
 
}

to:

public class Main extends Fragment{
 
 private TextView mTextView;
 private FragmentActivity mFrgAct;
 private Intent mIntent;
 private LinearLayout mLinearLayout;
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
          View root = inflater.inflate(R.layout.fragment_main, null);
        return root;
    }
 
 public void onViewCreated(View view, Bundle savedInstanceState) {
          // you can add listener of elements here
          /*Button mButton = (Button) view.findViewById(R.id.button);
            mButton.setOnClickListener(this); */
          mTextView = view.findViewById(R.id.mTextView);
          mLinearLayout = (LinearLayout)view;
 }
 public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
          mFrgAct = getActivity();
          mIntent = mFrgAct.getIntent(); //  Intent intent = new Intent(getActivity().getIntent());
 }
 
}
This entry was posted in Android Examples Code, Fragments Tutorial and tagged . Bookmark the permalink.


Comments are closed.

1,057 views