我試圖將數據從我的主活動傳遞到我的相機活動,傳遞到相機片段,然後返回到我的主活動。我希望用戶能夠保存他們在主要活動中輸入的信息,以便在完成使用相機時保留它們。將數據從片段傳遞到新活動
在我主要活動,我傳遞了如下的意圖額外的相機活動:
(注:本作品)
Intent cameraIntent = new Intent(MainActivity.this, CameraActivity.class);
cameraIntent.putExtra("description" , editTextDescription.getText().toString());
cameraIntent.putExtra("category" , editTextCategory.getText().toString());
cameraIntent.putExtra("notes" , editTextNotes.getText().toString());
MainActivity.this.startActivity(cameraIntent);
我則在收到臨時演員我的相機活動成功,並將它們傳遞到我的相機片段:
(注:本作品)
Intent intent = getIntent();
if (intent.getExtras() != null) {
description = intent.getExtras().getString("description");
category = intent.getExtras().getString("category");
notes = intent.getExtras().getString("notes");
}
if (null == savedInstanceState) {
CameraFragment cameraFragment = new CameraFragment();
Bundle bundle = new Bundle();
bundle.putString("description", description);
bundle.putString("category", category);
bundle.putString("notes", notes);
cameraFragment.setArguments(bundle);
Log.v(TAG, "Here is the bundle: " + bundle.toString());
getFragmentManager().beginTransaction()
.replace(R.id.container, cameraFragment.newInstance())
.add(cameraFragment, bundle.toString())
.commit();
}
在我相機片段,我收到了包在我onCreateView如下:
(注:本作品)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (getArguments() != null) {
description = getArguments().getString("description");
category = getArguments().getString("category");
notes = getArguments().getString("notes");
Log.v(TAG, "Here are your arguments from the camera activity: " + description + " " + category + " " + notes);
}
else {
Log.v(TAG, "Your arguments are null");
}
return inflater.inflate(R.layout.fragment_camera, container, false);
}
所有這些交易都正確執行,並且至此我已正確收到所有信息。我現在的目標是獲取信息,並將其傳回主要活動。
ISSUE **
case R.id.doneButton: {
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra("description", description);
intent.putExtra("category", category);
intent.putExtra("notes", notes);
Log.v(TAG, "Here are the extras going back to the Main Activity " + description + " " + category + " " + notes);
startActivity(intent);
break;
}
但是,我不能把我在片段接收到的信息,和其他地方使用的變量。我嘗試在除了onCreateView之外的方法中嘗試獲取參數,然後在將其他參數傳遞迴主要活動之前使用該方法。我也嘗試在我的onClick開關,大小寫方法中嘗試獲取參數,但是,當我嘗試將數據發送回我的主要活動時,它總是返回null,因爲它沒有接受它在參數中收到的內容,而是使用我之前聲明的字符串。
private String description;
private String category;
private String notes;
有沒有辦法把我在爭論中我相機片段接收到的數據,並使用它們的onCreateView方法之外,並送他們回到我主要活動?
所以你是說你有你的doneButton開關案例說明,類別和爲空筆記嗎? –