Communication between TabActivity and the embedded activity

后端 未结 5 829
Happy的楠姐
Happy的楠姐 2021-01-14 15:44

I am trying to figure out the best practice of communication between a TabActivity and the child activity embedded in this TabActivity.

In my TabActivity, there is a

5条回答
  •  长发绾君心
    2021-01-14 16:42

    Since TabActivity is an ActivityGroup, I would use one of the following:

    • getCurrentActivity()

    Returns the child tab activity being displayed. In your case, this method will return the instance of ChildActivity being used.

    ChildActivity childActivity = (ChildActivity) getCurrentActivity();
    
    • getLocalActivityManager().getActivity(String)

    Returns the child tab activity given its ID/tab spec name, whatever activity being displayed.

    ChildActivity childActivity = (ChildActivity) getLocalActivityManager().getActivity("Tab 1");
    

    I suggest overriding onNewIntent(Intent) in your ChildActivity:

    Intent intent = new Intent();
    intent.putExtra("xyz", "whatever"); // or a serializable
    
    ChildActivity childActivity = (ChildActivity) getLocalActivityManager().getActivity("Tab 1");
    childActivity.onNewIntent(intent);
    

    Let me know if it works!

提交回复
热议问题