我只是在学习Android开发。目前,我正在练习活动和片段之间的沟通,并处理他们的结果。我有一个关于以下情况的问题:ActivityA启动托管FragmentB的ActivityB,而ActivityA希望得到结果。我经常看到以下代码:
// ActivityA start ActivityB code
Intent i = new Intent(getActivity(), ActivityB.class);
i.putExtra(FragmentB.EDIT_TEXT, textView.getText());
startActivityForResult(i, REQUEST_CODE);
// FragmentB (hosted by ActivityB) set result code
Intent i = new Intent();
i.putExtra(EDIT_TEXT, value);
setResult(Activity.RESULT_OK, i);
finish();
// ActivityA handle result code
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
CharSequence val = data.getCharSequenceExtra(FragmentB.EDIT_TEXT);
textView.setText(val);
}
}
这对我来说看起来不是一个干净的方法,因为ActivityA必须知道ActivityB的实现细节 - 即主机FragmentB。我提出了一个更简洁的方法是在FragmentB和ActivityB之间放置一个接口:ActivityB实现了FragmentB的接口。当ActivityB可以完成自己时,FragmentB会调用此接口的方法(我尝试使代码尽可能短: - ):
public class ActivityA extends Activity {
private static final int REQUEST_CODE = 1;
private TextView textView;
private void startEditActivity() {
Intent i = new Intent(getActivity(), ActivityB.class);
i.putExtra(ActivityB.EDIT_TEXT, textView.getText());
startActivityForResult(i, REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
CharSequence val = data.getCharSequenceExtra(ActivityB.EDIT_TEXT);
textView.setText(val);
}
}
}
public class ActivityB extends Activity
implements FragmentB.OnEditSetListener
{
public final static String EDIT_TEXT = "nl.michiel.testapp.editTextActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CharSequence val = getIntent().getCharSequenceExtra(EDIT_TEXT);
getFragmentManager().beginTransaction()
.add(R.id.fragmentContainer, FragmentB.newInstance(val))
.commit();
}
}
@Override
public void onEditSet(CharSequence value) {
Intent i = new Intent();
i.putExtra(EDIT_TEXT, value);
setResult(Activity.RESULT_OK, i);
finish();
}
}
public class FragmentB extends Fragment {
public final static String EDIT_TEXT = "nl.michiel.testapp.editTextFragment";
private CharSequence value;
private EditText editText;
private OnEditSetListener editSetListener;
public interface OnEditSetListener {
public void onEditSet(CharSequence val);
}
public static FragmentB newInstance(CharSequence value) {
FragmentB fragment = new FragmentB();
Bundle args = new Bundle();
args.putCharSequence(EDIT_TEXT, value);
fragment.setArguments(args);
return fragment;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnEditSetListener) {
editSetListener = (OnEditSetListener)activity;
} else {
throw new ClassCastException(activity.toString()
+ " must implement OnEditSetListener");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
value = getArguments().getCharSequence(EDIT_TEXT);
}
// Called when edit has finished, for example after a button click
private void editFinished() {
editSetListener.onEditSet(editText.getText());
}
}
这是一个很好的解决方案,还是有一些常用的方法来处理这些情况?