我在Activity中有一个片段,如下所示,
public class ViewRecordFragment extends Fragment {
private Integer recordId;
public ViewRecordFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (recordId != null) {
new ViewRecordActivity.GetRecordToDisplay(getActivity()).execute(recordId);
loadImageResourcesAsync(recordId);
}
return inflater.inflate(R.layout.fragment_view_record, container, false);
}
public static ViewRecordFragment newInstance(Integer recordId) {
ViewRecordFragment fragment = new ViewRecordFragment();
Bundle bundle = new Bundle();
bundle.putInt(BundleHelper.RECORD_ID, recordId);
fragment.setArguments(bundle);
fragment.setRetainInstance(true);
return fragment;
}
private void loadImageResourcesAsync(Integer recordId) {
new LoadImageResourcesAsync(getActivity()).execute(recordId);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
if (getArguments() != null) {
recordId = getArguments().getInt(BundleHelper.RECORD_ID);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_view_record, menu);
//super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.action_new_photo:
//Integer recordId = getArguments().getInt(BundleHelper.RECORD_ID);
Intent intent = new Intent(getActivity(), CameraActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle bundle = new Bundle();
bundle.putInt(BundleHelper.RECORD_ID, recordId);
bundle.putString(BundleHelper.UNIQUE_IDENTIFIER, UUID.randomUUID().toString());
intent.putExtras(bundle);
getActivity().startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
并且活动使用此片段显示单个记录
public class ViewRecordActivity extends AppCompatActivity {
private Toolbar toolbar = null;
private Integer recordId;
private String patientNo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_record);
toolbar = (Toolbar) findViewById(R.id.header);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Record");
Bundle extras = getIntent().getExtras();
if (extras != null) {
recordId = extras.getInt(BundleHelper.RECORD_ID);
patientNo = extras.getString(BundleHelper.PATIENT_NO);
getSupportActionBar().setSubtitle("Patient Id: " + patientNo);
}
if (savedInstanceState == null) {
ViewRecordFragment viewRecordFragment = ViewRecordFragment.newInstance(recordId);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.replace(R.id.view_record_fragment, viewRecordFragment
).commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public static class GetRecordToDisplay extends GetRecordAsync {
private TextView dos;
private TextView facultyName;
private TextView patientName;
private TextView patientId;
public GetRecordToDisplay(Activity activity) {
super(activity);
this.dos = (TextView) activity.findViewById(R.id.date_submitted_text);
this.facultyName = (TextView) activity.findViewById(R.id.name_text);
this.patientName = (TextView) activity.findViewById(R.id.customer_name_text);
this.patientId = (TextView) activity.findViewById(R.id.customer_id_text);
}
@Override
protected void onPostExecute(Customer customer) {
if (customer != null) {
this.dos.setText(customer.getDOS_D());
this.facultyName.setText(customer.getDOS_R());
this.patientName.setText(customer.getCUSTOMER_N());
this.patientId.setText(customer.getCUSTOMER_C());
}
}
}
当我尝试从单击菜单项启动新活动时,会出现问题。单击菜单项时,我传递的recordId
为null。
我不确定我在这里失踪了什么。我可以看到在Activity中创建片段(在调试模式下)时会分配recordId
。
但是当我单击菜单项并转到特定菜单项的情况时,recordId
为空。但我期待在创建片段时分配给它的recordId。
答案 0 :(得分:1)
您正在为片段设置参数,但您没有使用它们。在片段的onCreate
方法中执行此操作:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
recordId = getArguments().getInt(BundleHelper.RECORD_ID);
}