Android:当Fragments视图准备好时,如何通知Activity? [英] Android: how to notify Activity when Fragments views are ready?

查看:92
本文介绍了Android:当Fragments视图准备好时,如何通知Activity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个活动中使用Google Maps API V2,该活动具有下拉导航,其中地图位于第二位置.

I'm using Google Maps API V2 in an activity wih drop down navigation where the map is on the second position.

我正在按实际的方式添加地图:

I'm adding the map pragmatically like:

mMapFragment = supportMapFragment.newInstance();
getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.placeHolder, mMapFragment, TAG_MAP)
        .commit(); 

我想获得GoogleMap ovject,作为文档 https://developers.google .com/maps/documentation/android/map 表示应使用mMapFragment.getMap()完成,但返回null.

I want to get the GoogleMap ovject, as the documentation https://developers.google.com/maps/documentation/android/map say it should be done with mMapFragment.getMap(), but it returns null.

根据 http://developer.android. com/reference/com/google/android/gms/maps/SupportMapFragment.html 如果Fragment尚未经历onCreateView生命周期事件,则返回null.

according to http://developer.android.com/reference/com/google/android/gms/maps/SupportMapFragment.html it returns null if the Fragment hasn't gone through the onCreateView lifecycle event.

我怎么知道片段准备好了?

how can I know whenever the fragment is ready ?

我发现此重写onActivityCreated似乎是一个解决方案,但是随后我将不得不通过构造函数而不是newInstance()实例化片段,这有什么区别吗?

Overriding onActivityCreated seems like a solution, but then I'll have to instantiate the fragment through a constructor and not with newInstance(), does it make any difference ?

推荐答案

我的首选方法是使用回调从Fragment获取信号.此外,这是Android在与活动进行通信中建议的推荐方法.

My preferred method is to use a callback to get a signal from a Fragment. Also, this is the recommended method proposed by Android at Communicating with the Activity

例如,在您的Fragment中,添加一个接口并进行注册.

For your example, in your Fragment, add an interface and register it.

public static interface OnCompleteListener {
    public abstract void onComplete();
}

private OnCompleteListener mListener;

public void onAttach(Context context) {
    super.onAttach(context);
    try {
        this.mListener = (OnCompleteListener)context;
    }
    catch (final ClassCastException e) {
        throw new ClassCastException(context.toString() + " must implement OnCompleteListener");
    }
}

现在在Activity

public class MyActivity extends FragmentActivity implements MyFragment.OnCompleteListener {
    //...

    public void onComplete() {
        // After the fragment completes, it calls this callback.
        // setup the rest of your layout now
        mMapFragment.getMap()
    }
}

现在,无论您的Fragment中的任何内容表示已加载,请通知您的Activity它已准备就绪.

Now, whatever in your Fragment signifies that it's loaded, notify your Activity that it's ready.

@Override
protected void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // create your fragment
    //...

    // signal that you're done and tell the Actvity to call getMap()
    mListener.onComplete();
}

编辑2017-12-05 onAttach(活动活动)为

EDIT 2017-12-05 onAttach(Activity activity) is deprecated, use onAttach(Context context) instead. Code above adjusted.

这篇关于Android:当Fragments视图准备好时,如何通知Activity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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