简体   繁体   English

将结果从子活动传递到“父选项卡活动”

[英]Pass results from child activity to Parent Tab Activity

I have 6 activities in my app which are Activity1, Activity2, TabActivity, childTab1, childtab2, ExtraActivity. 我的应用程序中有6个活动,分别是Activity1,Activity2,TabActivity,childTab1,childtab2,ExtraActivity。

App flow: 应用流程:

Activity1 => Activity2 => TabActivity =>ChildTab1 or ChildTab2 => ExtraActivity

Now i want to pass some data from ExtraActivity to Activity1 . 现在我想将一些数据从ExtraActivity传递给Activity1 I am using StartActivityForResult for starting all activities. 我正在使用StartActivityForResult启动所有活动。

ExtraActivity is able to send data to ChildTab1 or ChildTab2 but i unable to send that data back to TabActivity and further back to Activity2 and finally to Activity1 . ExtraActivity可以将数据发送到ChildTab1ChildTab2但是我无法将该数据发送回TabActivity ,再进一步发送回Activity2 ,最后再发送给Activity1

Please help!! 请帮忙!!

Update: While searching, i got my solution via this post: 更新:搜索时,我通过这篇文章得到了解决方案:

How to return a result (startActivityForResult) from a TabHost Activity? 如何从TabHost活动返回结果(startActivityForResult)?

I think you can delegate the onActivityResult method-result from ChildTabX to TabActivity with ((TabActivity)getParent()).onActivityResult(). 我认为您可以使用((TabActivity)getParent())。onActivityResult()将ChildTabX的onActivityResult方法结果委托给TabActivity。 There you just call setResult and finish() again to send the result to Activity2. 在那里,您只需调用setResult和finish()即可将结果发送到Activity2。 Same for Activity2 与Activity2相同

You could use the application context to pass data between activites. 您可以使用应用程序上下文在活动之间传递数据。

Way to use app context. 使用应用上下文的方式。

Extend the application class and add the attributes as you require. 扩展应用程序类并根据需要添加属性。 So in your activity you can access the application context and get the data. 因此,在您的活动中,您可以访问应用程序上下文并获取数据。 As the application context is a singleton it will be the same instance in every activity. 由于应用程序上下文是单例,因此在每个活动中它都是相同的实例。

MyApplication appContext = (MyApplication) getApplicationContext();
appContext.myString = "YOUR DATA;

In any other activity you can access that bitmap the same way. 在任何其他活动中,您都可以以相同的方式访问该位图。

MyApplication appContext = (MyApplication) getApplicationContext();

Now the string is in the appContext object. 现在,该字符串位于appContext对象中。

You also need to add 您还需要添加

android:name=".MyApplication"

to application tag in the manifest file. 清单文件中的应用程序标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM