我的应用中有一个选项菜单,显示用户在地图上添加的所有标记的列表。使用ListView表示标记,当用户选择该ListView的一个项目时,我想返回(恢复?)到主活动,将标记位置作为参数传递,但不创建新活动。那可能吗?现在我在用户点击任何项目时创建一个新活动,是否可以简单地回到主要活动?
MarkersList活动代码:
mainListView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View v, int position, long id)
{
Intent intent = new Intent(getBaseContext(), MainActivity.class);
MyMarkerObj marker = (MyMarkerObj)table.get(position);
intent.putExtra("position", marker.getId());
startActivity(intent);
finish();
}
});
MainActivity代码:
@Override
protected void onResume() {
Intent intent = getIntent();
if (intent != null) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
int reportId = bundle.getInt("position");
Toast.makeText(getApplicationContext(), Integer.toString(reportId), Toast.LENGTH_SHORT).show();
try {
data.open();
} catch (Exception e) {
Log.i("hello", "hello");
}
//Get the marker selected in the listview
String position = data.getMarkerById(reportId);
//System.out.println(">>>>>>>>>>>>>>> position = " + position);
}
}
super.onResume();
}
答案 0 :(得分:2)
使用StartActivityForResult(intent,reqcode)
代替startActivity(intent)
。
首先在你的 第一项活动
Intent i=new Intent(MainActivity.this, MarkerActivity.class);
startActivityForResult(i,1); <-- 1 is request code, you can give anything.
然后是第二个Activity的ItemClick
Intent intent = getIntent();
MyMarkerObj marker = (MyMarkerObj)table.get(position);
intent.putExtra("position", marker.getId());
setResult(responsecode,intent);
finish();
并在
FirstActivity的onActivityForResult
得到像这样的值
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == 1 && resultCode == RESULT_OK)
{
Bundle bun=data.getExtras();
int position= bun.getInt("position");
}
}