我正在尝试将数据从我的主要活动,我的相机活动,我的相机片段,然后回到我的主要活动。我希望用户能够将他们在主要活动上输入的信息保存在完成使用相机时保留。
在我的主要活动上,我将以下内容作为意图附加内容传递给相机活动:
(注意:这有效)
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 开关,case方法中获取参数,但是,当我尝试将数据发送回主要活动时,它总是返回null,因为它没有采用它在论证中得到的东西,而是使用我之前声明过的字符串。
private String description;
private String category;
private String notes;
有没有办法在我的相机片段中获取我在参数中收到的数据,并在 onCreateView 方法之外使用它们,并将它们发送回我的主要活动?
答案 0 :(得分:0)
为什么不做一个' get'片段中的方法。
cameraFragment.getXXXX();
答案 1 :(得分:0)
我有点改写你的代码,这很有效。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent cameraIntent = new Intent(MainActivity.this, CameraActivity.class);
cameraIntent.putExtra("description" , "description");
cameraIntent.putExtra("category" , "category");
cameraIntent.putExtra("notes" , "notes");
startActivity(cameraIntent);
}
}
public class CameraActivity extends Activity{
private String description,category,notes;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
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)
.commit();
}
}
}
即使在doneButton的onClick中,也会填充3个字符串
public class CameraFragment extends Fragment{
private String description,category,notes;
private Button doneButton;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_camera, container, false);
if (getArguments() != null) {
description = getArguments().getString("description");
category = getArguments().getString("category");
notes = getArguments().getString("notes");
Log.v("CameraFragment", "Here are your arguments from the camera activity: " + description + " " + category + " " + notes);
}
else {
Log.v("CameraFragment", "Your arguments are null");
}
doneButton = view.findViewById(R.id.doneButton);
doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra("description", description);
intent.putExtra("category", category);
intent.putExtra("notes", notes);
Log.v("CameraFragment", "Here are the extras going back to the Main Activity " + description + " " + category + " " + notes);
startActivity(intent);
}
});
return view;
}
}