伙计们,我对java android编程很新(这是我制作的第一个应用程序),我有这个问题: 如何从另一个从主要活动中调用的活动刷新我的主活动? 如果你给我一些例子,我也很感激,因为我还没有定位......
答案 0 :(得分:0)
您可以使用startActivityForResult()
代替context.startActivity()
调用context.startActivityForResult()
,然后在结束前将结果设置为已启动的活动。
以下是documentation for 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 :(得分:0)
如何从子活动中调用主要活动。使用界面。
<强> 1。 Creata子活动中的公共界面
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部分。你可以称之为听众或回电。只需右键单击错误并选择添加引用,添加未实现的方法实际上没有太多的按键来执行此操作。界面可能是协调团队努力制定下一件大事的最简单方法。