![](/img/trans.png)
[英]How to pass value from parent activity to child activity and on child activity exit, refresh the parent activity
[英]Refreshing parent activity from child?
伙计们,我是Java android编程的新手(这是我正在制作的第一个应用程序),我有一个问题:如何从另一个活动(从主要活动中调用)刷新我的主要活动? 如果您给我一些例子,我也将不胜感激,因为我仍然不太适应...
您可以为此目的使用startActivityForResult()
,而不是context.startActivity()
调用context.startActivityForResult()
,然后在完成活动之前在启动的活动中设置结果。
这是Context.startActivityForResult()
的文档
样例代码:
// Activity A
public class ActivityA extends Activity {
private static final int REQUEST_CODE_ACTIVITY_B_FOR_RESULT = 1; // or other int value
// sample code which starts Activity B
private void onSomeButtonClick() {
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, REQUEST_CODE_ACTIVITY_B_FOR_RESULT);
}
// this method will be called when started activity finished
// and returned some result of its work
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
super.onActivityResult(requestCode, resultCode, resultData);
if (requestCode == REQUEST_CODE_ACTIVITY_B_FOR_RESULT) {
if (resultCode == RESULT_OK) {
// handle result ok and resultData here
} else {
// handle result canceled or other resultCode and its resultData here
}
}
}
}
// Activity B
public class ActivityB extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED); // by default result of starting activity is negative
}
// some code which doing some action and setting result as ok
private void doSomething() {
Intent resultData = new Intent();
resultData.putExtra("SOME_EXTRA", "did it"); // or other result data
setResult(RESULT_OK, resultData);
finish(); // finishing this activity, result code and result data will be accessible in previous activity
}
}
如何从子活动中调用主要活动。 使用界面。
1.在子活动中创建公共接口
public class MapSettings extends DialogFragment implements
OnCheckedChangeListener {
public interface BestRidesSettingsDialogListener {
void onMapSettingsChange(int mapType);
}
2.在主要活动注释中实现界面,右键单击错误,然后选择添加导入,添加引用,添加未实现的方法。
public class KmlReader extends ActionBarActivity implements
BestRidesSettingsDialogListener {
3.当子活动开始时,在子活动中将getActivity强制转换为公共接口
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// the activity may be null if this is called without implementing the
// BestRidesSettingsDialogListener (The settings object saves the
// setting so the
// call back may not be needed.
activity = (BestRidesSettingsDialogListener) getActivity();
4.然后在子活动中,单击按钮或发生某些事件时,调用接口方法。
@Override
public void onCheckedChanged(RadioGroup rg, int checkId) {
// TODO Auto-generated method stub
int mapType = 0;
switch (checkId) {
case R.id.RDORoad:
mapType = GoogleMap.MAP_TYPE_NORMAL;
break;
case R.id.RDOHybrid:
mapType = GoogleMap.MAP_TYPE_HYBRID;
break;
case R.id.RDOSatelite:
mapType = GoogleMap.MAP_TYPE_SATELLITE;
break;
case R.id.RDOTerrain:
mapType = GoogleMap.MAP_TYPE_TERRAIN;
break;
}
// run the activity onchange
// if the activity is null there is no listener to take action on the
// settings
if (activity != null) {
activity.onMapSettingsChange(mapType);
}
5.如果您已在主活动中填写了未实现的方法存根,则直接在子活动中的主活动中执行代码。
@Override
public void onMapSettingsChange(int mapType) {
if (mMap != null) {
mMap.setMapType(mapType);
}
}
注释边栏:接口一直是我最喜欢的Java部分。 您可以将其称为侦听器或回叫。 只需右键单击错误并选择添加引用,添加未实现的方法,实际上并没有太多的击键操作。 该界面也许是协调团队工作的下一个重大任务的最简单方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.