将值返回到其他活动 [英] Return value to a different Activity

查看:134
本文介绍了将值返回到其他活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个应用程序它基本上看起来像附图中的设计。
如你所见,有4个活动,一些活动有碎片。



我想得到一个测试的答案回到用户的个人资料
到目前为止,我已经将结果上传到服务器,并且每当他们返回到 ProfileActivity 时,应用程序会更新用户的个人资料,但这似乎浪费资源。



在应用程序中是否有模式或方法?
我查看了这个:这似乎可行,如果我可以以某种方式链接两个 startActivityForResult() s。



我研究了使用这个问题,但我无法获取 UserFragment 的实例到 TestActivity



我希望有人能指示我正确的方法。

$ b $一个方法是使用 startActivityForResult()不完成任何活动



通过 startActivityForResult()开始所有活动,然后根据您的条件,您可以完成活动



将结果传回以前的活动,使用 onActivityResult()



然后对于片段,您可以在 ProfileActivity 中存储片段对象。
在用于更新UI的片段写入方法中



所以你可以使用片段对象访问该方法

  class ProfileActivity extends ... 
{
@override
public void onActivityResult(....)
{
.....
frgamnetObject.updateUI();
....
}
}


I have an app that basically looks like the design in the attached picture. As you can see, there are 4 activities, and some of the activities have fragments.

I want to get the answer of a test back to the user's profile. Up to now, I've been uploading the result to the server and having the app update the user's profile every time they go back to the ProfileActivity, but that seems like a waste of resources.

Is there a pattern or way to do this in the app? I looked at this: which seems doable if I can somehow link two startActivityForResult()s.

I looked into using the delegate pattern described in this question but I can't get the instance of the UserFragment to the TestActivity.

I hope someone can point me in the direction of the correct way of doing this.

解决方案

One approach is use startActivityForResult() don't finish any Activity

start all activities by startActivityForResult() then depending on your condition you can finish activity

pass result back to the previous activity using onActivityResult()

Then for fragment you can store fragment object in ProfileActivity . In Fragment write method for updating UI

So you can access that method using fragment object

class ProfileActivity extends ......
{
  @override
  public void onActivityResult(....)
  {
    .....
    frgamnetObject.updateUI();
    ....
  }
}

这篇关于将值返回到其他活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆