这是我的应用程序的导航:
This is the navigation of my application:
活动1
要求活性2
→ Activity2.finish()
,呼叫 Activity3
→ Activity3.finish()
Activity1
calls Activity2
→ Activity2.finish()
, call Activity3
→ Activity3.finish()
在 Activity3
完成后,它会调用活动1 。现在我该怎么妙传
Activity3
的值活动1
?
When Activity3
finishes, it calls the onResume
method of Activity1
. Now how can I pass a value from Activity3
to Activity1
?
Umesh制作显示出良好的技术,但我想你想的相反方向。
Umesh shows a good technique but I think you want the opposite direction.
第1步
在开始活动2和3,使用 startActivityForResult
。这可以让你处理的结果,在调用活动。
When starting Activity 2 and 3, use startActivityForResult
. This allows you handle the result in the calling activity.
startActivityForResult(MY_REQUEST_ID);
第2步
在活动2和3,通话的setResult(INT,意图)
返回值:
In Activities 2 and 3, call setResult(int, Intent)
to return a value:
Intent resultData = new Intent();
resultData.putExtra("valueName", "valueData");
setResult(Activity.RESULT_OK, resultData);
finish();
第3步
在您的通话activty,实施 onActivityResult
和获取数据:
In your calling activty, implement onActivityResult
and get the data:
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == MY_REQUEST_ID) {
if (resultCode == RESULT_OK) {
String myValue = data.getStringExtra("valueName");
// use 'myValue' return value here
}
}
}
编辑:
技术#2
是的,你也可以通过添加类应用程序,扩展应用程序
使用全局应用程序的状态,看到这个计算器的 answer
Yes, you can also use global application state by adding a class to your application that extends Application
, see this StackOverflow answer