实例化片段的不同方法

时间:2018-12-30 19:21:32

标签: android android-fragments fragment

在下面的代码中,我想知道实例化静态片段之间的区别,如下面的代码_1所示。 我发布了片段的代码,如下面的code_2部分所示。

请让我知道两种实例化之间的区别,以及何时使用它们。

代码_1

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

代码_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++);
}
}

1 个答案:

答案 0 :(得分:1)

在第一行中,您将片段转换为StudentMVCView类型,因此您可以访问添加到其中的额外成员,例如setTextViewValueFor(int id)setEditTextValueFor(String str),.. etc < / p>

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

在第二行中,您得到的片段是它的超级类型,它是Android框架的Fragment类型,在这种情况下,您无法访问StudentMVCView类型的这些额外成员

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