user10776303
user10776303

Reputation: 241

Different ways of instantiating a fragment

In the below code, I would like to know the difference between instantiating a static fragment as shown in code_1 below. I posted the code of the fragment as shown in code_2 section below.

please let me know the difference between the two types of instantiaition, and when to use each of them.

code_1:

  StudentMVCView mStudentMVCViewInitializedInstance = (StudentMVCView) this.getSupportFragmentManager().findFragmentById(R.id.mvc_view_fragment);
  Fragment mStudentMVCViewFragmentInitializedInstance = this.getSupportFragmentManager().findFragmentById(R.id.mvc_view_fragment);

code_2:

public class StudentMVCView extends Fragment implements View.OnClickListener{

private final static String TAG_LOG = StudentMVCView.class.getSimpleName();
private View mMainView = null;
private TextView mTextViewValue = null;
private EditText mEditTextValue = null;
private Button mBtn = null;
private TextView mTextViewBtnValue = null;
private StudentMVCModel mStudentMVCModel = null;
private int counter = 1;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Log.v(TAG_LOG, "onCreateView");
    this.mMainView = inflater.inflate(R.layout.mvc_view_layout, null);
    this.mTextViewValue = this.mMainView.findViewById(R.id.mvc_view_textView_value);
    this.mEditTextValue = this.mMainView.findViewById(R.id.mvc_view_editText_value);
    this.mBtn = this.mMainView.findViewById(R.id.mvc_view_button);
    this.mBtn.setOnClickListener(this);
    this.mTextViewBtnValue = this.mMainView.findViewById(R.id.mvc_view_textView_btnValue);

    return this.mMainView;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Log.v(TAG_LOG, "onViewCreated");
    view.findViewById(R.id.mvc_view_textView_value);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.v(TAG_LOG, "onActivityCreated");
    Bundle bundledArgs = this.getArguments();
    StudentMVCModel studentMVCModel = (StudentMVCModel) this.getSerializedfromBundle(bundledArgs, "p");
    Log.v(TAG_LOG, "studentMVCModel.getStudentId()" +  studentMVCModel.getStudentId());

    this.setStudentMVCModelObject(studentMVCModel);
}

private void setStudentMVCModelObject(StudentMVCModel studentMVCModel) {
    this.mStudentMVCModel = studentMVCModel;
}

private StudentMVCModel getStudentMVCModelObject() {
    return this.mStudentMVCModel;
}
private Bundle getBundledArguments() {
    Log.d(TAG_LOG, "getBundledArguments");
    if (this.getArguments() !=null) {
        return this.getArguments();
    } else {
        Log.e(TAG_LOG, "this.getArguments() is NULL.");
        throw new NullPointerException("getArguments is NULL");
    }
}
private Object getSerializedfromBundle(Bundle bundle, String key) {
    Log.d(TAG_LOG, "getSerializedfromBundle");
    if (bundle != null) {
        return bundle.get(key);
    } else {
        Log.e(TAG_LOG, "bundle is NULL.");
        throw new NullPointerException("bundle is null");
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.v(TAG_LOG, "onDestroy");
}

public void setStudentIdToView(int id) {
    if (this.mMainView != null) {
        TextView textView = this.mMainView.findViewById(R.id.mvc_view_textView_value);
        Log.v(TAG_LOG, "TextView contains: " + textView.getText().toString());
    }
}

public void setTextViewValueFor(int id) {
    if (this.mTextViewValue != null) {
        this.mTextViewValue.setText("" + id);
    } else {
        Log.e(TAG_LOG, "setTextViewValueFor is NULL.");
    }
}

public void setEditTextValueFor(String str) {
    if (this.mEditTextValue != null) {
        this.mEditTextValue.setText(str);
    } else {
        Log.e(TAG_LOG, "mEditTextValue is NULL.");
    }
}

public void clearEditText() {
    if (this.mEditTextValue != null) {
        this.mEditTextValue.setText("");
    } else {
        Log.e(TAG_LOG, "mEditTextValue is NULL.");
    }
}

@Override
public void onClick(View v) {
    int id = this.getStudentMVCModelObject().getStudentId();
    Log.i(TAG_LOG, "onClick: id: " + id + " counter: " + counter++);
}
}

Upvotes: 1

Views: 38

Answers (1)

y.allam
y.allam

Reputation: 1516

In the first line You're casting the fragment to StudentMVCView type, so you're able to access extra members added to it like setTextViewValueFor(int id), setEditTextValueFor(String str), ..etc

  StudentMVCView mStudentMVCViewInitializedInstance = (StudentMVCView) this.getSupportFragmentManager().findFragmentById(R.id.mvc_view_fragment);

In the second line you're getting the fragment as it's super type which is the Android framework's Fragment type, in this case you can't access these extra members in StudentMVCView type

  Fragment mStudentMVCViewFragmentInitializedInstance = this.getSupportFragmentManager().findFragmentById(R.id.mvc_view_fragment);

Upvotes: 1

Related Questions