不同活动之间的沟通

时间:2017-02-27 11:45:24

标签: android

目前正在开发Android应用程序,这就像Facebook一样是社交应用程序。 我要做的是如何显示当用户从家庭供稿页面到供稿细节,然后从那里到另一个供稿相关页面,如同那里的帖子。

例如,我们考虑一下:

A -> Feeds page(feed id 1, with image containing two person me and my friend)
B -> Feeds Details Page(Showing that image)
C -> My friend's profile

用户从Feed页面导航到我朋友的个人资料,就像那里的图像一样。当用户再次回到Feed页面时,我应该如何渲染。

我尝试通过活动获得结果,但这对多个活动来说效率不高。

2 个答案:

答案 0 :(得分:1)

一种方法是使用本地广播

例如,在Feed活动中:

LocalBroadcastManager.getInstance(getApplicationContext())
    .registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //update your UI here
        }
    }, new IntentFilter("photoLiked"));

当喜欢照片时发送广播:

 LocalBroadcastManager.getInstance(getApplicationContext())
       .sendBroadcast(new Intent("photoLiked"))

答案 1 :(得分:0)

您可以使用像Otto这样的事件总线。

使用方便,一切都很松散。

http://square.github.io/otto/

相关问题