Android api v15中的活动互连

时间:2014-08-21 11:16:32

标签: android

我有2个活动:第一个活动whith Button,它按意图调用第二个活动(打开文件对话框)。如何将文件名从第二个活动返回到第一个?通过意图?哪种方式值得使用?它需要使用api <= 15。

1 个答案:

答案 0 :(得分:2)

请参阅此回答here

要开始其他活动,只需要根据您的情况获得活动的回复,您可以使用:startActivityForResult(Intent intent, int identifier_value);。在第二个活动中,您可以选择返回结果或取消结果。

文档声明:

    The startActivity(Intent) method is used to start a new activity, which will be placed 
at the top of the activity stack. It takes a single argument, an Intent, which describes
the activity to be executed.

    Sometimes you want to get a result back from an activity when it ends. For example,
you may start an activity that lets the user pick a person in a list of contacts; when it 
ends, it returns the person that was selected. To do this, you call the 
startActivityForResult(Intent, int) version with a second integer parameter identifying 
the call. The result will come back through your onActivityResult(int, int, Intent) method.

    When an activity exits, it can call setResult(int) to return data back to its parent.
It must always supply a result code, which can be the standard results RESULT_CANCELED,
RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can 
optionally return back an Intent containing any additional data it wants. All of this 
information appears back on the parent's Activity.onActivityResult(), along with the 
integer identifier it originally supplied.