简体   繁体   English

Android Intent显示新活动后回调

[英]Callback once Android Intent shows new Activity

I have two activities: Activity A and Activity B. 我有两个活动:活动A和活动B。

Activity A shows Activity B via an Intent. 活动A通过意图显示活动B。

Activity A displays a number of fragments, each of which may display Activity B using said Intent. 活动A显示许多片段,每个片段都可以使用所述Intent显示活动B。

Let's say I have Fragments X, Y & Z and when Activity B is shown from Fragment Y then I want to be run some code when Activity B is finally showing. 假设我有片段X,Y和Z,并且当从片段Y显示活动B时,我想在活动B最终显示时运行一些代码。 The code I run will also need a reference to Activity A. 我运行的代码还需要引用活动A。

Can this be done? 能做到吗?

It is bad idea. 这是个坏主意。

If you run new activity, Activity B, then previous activity (Activity A) and it's fragments are already saved their's states. 如果您运行新的活动,即活动B,则先前的活动(活动A)及其片段已保存其状态。 Their's onSaveInstance and onPause methods are already called and you should not modify any views in activity/fragment after onSaveInstance. 它们的onSaveInstance和onPause方法已经被调用,并且在onSaveInstance之后,您不应在活动/片段中修改任何视图。 If you modify views after onSaveInstance , those changes will not get saved to saved state. 如果您在onSaveInstance之后修改视图,则这些更改将不会保存到保存状态。 Android can destroy activity any time after onPause and restore it later from saved state. Android可以在onPause之后的任何时间销毁活动,并稍后从保存状态中恢复活动。

If you don't need to change any views of fragment, consider passing some data into Activity B and doing something in it. 如果您不需要更改片段的任何视图,请考虑将一些数据传递到活动B中并在其中进行一些操作。

For more details on activity destroying/restoring read here https://developer.android.com/training/basics/activity-lifecycle/recreating.html 有关活动销毁/恢复的更多详细信息,请阅读此处https://developer.android.com/training/basics/activity-lifecycle/recreating.html

For your use case, make Activity B a fragment. 对于您的用例,将活动B片段化。 This way you can access activity A from fragment B through getActivity(). 这样,您可以通过getActivity()从片段B访问活动A。

I think peeps overcomplicated this. 我认为窥视使这一问题变得过于复杂。 Just have some callback that tells your activity when to start Activity B from Fragments X, Y, or Z. Maybe do it in onAttach . 只需提供一些回调,告诉您的活动何时从片段X,Y或Z启动活动B。也许可以在onAttach

Ex: 例如:

Fragment X 片段X

private MyCallback _myCallback;
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof MyCallback){
        _myCallback =(MyCallback) context;
    }
}

Now whenever something happens that you need to start Activity B, just do: 现在,无论何时发生什么事情都需要启动活动B,只需执行以下操作:

// Still in Fragment X
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("bloop", "frag_x"); // or "frag_y", "frag_z"
_myCallback.startActivityB(intent)

Now in Activity B just check the value of bloop in the arguments and BAM. 现在在活动B中,只需检查参数和BAM中的bloopbloop You know where it came from. 你知道它来自哪里。 I intentionally left some things out as an exercise. 我故意省略了一些东西作为练习。 You can definitely make this more maintainable. 您绝对可以使它更易于维护。 Maybe use those OO skills to not duplicate the code that grabs that callback, etc. 也许使用那些OO技能不复制获取该回调的代码,等等。

Common method to resolve the problem is via using callback. 解决此问题的常用方法是使用回调。 But there is major drawback with callback. 但是回调有一个主要缺点。 You need to putting callback code here and there and ending tight coupling your codes. 您需要在此放置回调代码,并结束紧密耦合代码。 And then you've got a more problem just to make sure you have the 'right' callback. 然后,要确保您具有“正确的”回调,就会遇到更多问题。

My suggestion is to use EventBus system to decouple your code. 我的建议是使用EventBus系统解耦您的代码。

You just need to register Activity to receive Event. 您只需要注册Activity即可接收Event。

Here a scenario with your case: 这是您的情况的一种情况:

  1. Fragment from Activity A launch Activity B called via intent. 来自活动A的片段通过意图调用了活动B。
  2. Activity B has shown, inform Activity A. Send MessageEvent that Activity B has shown. 活动B已显示,通知活动A。向MessageEvent发送活动B已显示。

in Activity B onCreated: 在活动B中onCreated:

EventBus.getDefault.post(new ActivityBHasShownEvent());

in Activity A, receive the message: 在活动A中,收到以下消息:

@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this); // Subscribe to event.
...
}

@Subscribe public void onMessageEvent(ActivityBHasShownEvent event) {
   //Do something after activity B shown
}
  1. Done. 做完了

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

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