这是我的应用程序的导航:
This is the navigation of my application:
Activity1 调用 Activity2 →Activity2.finish(),调用Activity3 →Activity3.finish()
Activity1 calls Activity2 → Activity2.finish(), call Activity3 → Activity3.finish()
当Activity3结束时,它调用Activity1的onResume方法.现在如何将值从 Activity3 传递到 Activity1?
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.
第一步
启动 Activity 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, Intent) 返回一个值:
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
在您的调用活动中,实现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
是的,您还可以通过向您的应用程序添加一个扩展 Application 的类来使用全局应用程序状态,请参阅此 StackOverflow 答案
Yes, you can also use global application state by adding a class to your application that extends Application, see this StackOverflow answer
更多推荐
如何在 Android 上的活动之间传递值?
发布评论