在android中离开当前活动后显示上次更新的活动

时间:2015-05-08 05:03:41

标签: android android-intent android-activity

我正在使用一个带有ArcGIS地图的编辑工具。首先我启动我的MainActivity,用户可以在地图上绘制一些图纸。另一个图纸想要从材料设计导航抽屉的列表菜单中做,有一个叫做的项目单击该项后,它转到另一个名为文件浏览器的活动。从那里选择相关文件后,我想用编辑过的图纸启动MainActivity。 在我的MainActivity中;

 public void onNavigationDrawerItemSelected(int position) {
       switch (position) {
            case 1: {
                Intent intent = new Intent(MainActivity.this, filechooser.class);
                startActivity(intent);
                break;
            }

            default:
                break;

        }
}

在FileChooser活动中;

//Here selecting relevent file for the drawings.Really here selecting file which contains relevent coordinates for drawings on the map and pass the file path to MainActivity.Map is on the MainActivity.This activity contains only a file browser.
protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Option o = adapter.getItem(position);


        if (o.getdata().equalsIgnoreCase("folder") ||           o.getdata().equalsIgnoreCase("Parent Directory")) {
            direction = new File(o.getPath());
            fill(direction);
        } else {
            filepath = o.getPath();
            Intent intent2 = new Intent(filechooser.this, MainActivity.class);
            //but here i want to go to MainActivity with previous drawings not just to want restart activity.
            intent2.putExtra("path", filepath);
            startActivity(intent2);
            
        }
    }

我可以这样做吗?如果是这样,请帮助我。感谢我的英语:(

1 个答案:

答案 0 :(得分:0)

MainActivity应使用FileChooser启动startActivityForResult()。然后,FileChooser会创建一个Intent,用于将数据返回到MainActivity。要将此数据传回MainActivityFileChooser只需要调用setResult()然后调用finish()(而不是调用startActivity())。这将关闭FileChooser并在onActivityResult()中致电MainActivity

编辑添加代码示例

MainActivity

public void onNavigationDrawerItemSelected(int position) {
   switch (position) {
        case 1: {
            Intent intent = new Intent(MainActivity.this, filechooser.class);
            startActivityForResult(intent, 1);
            break;
        }
    }
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            // "data" is the Intent that filechooser sent back
            // you can now do data.getStringExtra(...) or whatever to
            // get the data
            String filepath = data.getStringExtra("path");
        } else {
            // filechooser failed for whatever reason
        }
    }
}

filechooser

protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Option o = adapter.getItem(position);
        if (o.getdata().equalsIgnoreCase("folder") ||           o.getdata().equalsIgnoreCase("Parent Directory")) {
            direction = new File(o.getPath());
            fill(direction);
        } else {
            filepath = o.getPath();
            Intent intent2 = new Intent();
            //but here i want to go to MainActivity with previous drawings not just to want restart activity.
            // return filepath
            intent2.putExtra("path", filepath);
            setResult(RESULT_OK, intent2);
            // close this activity return to MainActivity
            finish();
        }
    }