Google MapView添加了正常的生命周期

时间:2014-05-26 08:32:30

标签: android google-maps-android-api-2 android-lifecycle

我遇到了Googles MapView Activity的麻烦。

我遇到问题的原因:

我的应用程序依赖于许多异步事件。当我启动一个包含Map的Activity时,我有一个模板机制,它添加并初始化Map并将所有活动回调(如onCreate(),onResume()中继到地图。一切正常!

但是当从后台恢复此活动时,我的模板机制尚未准备好在onCreate()中创建动态布局,因此我无法中继Map所需的生命周期回调。在最坏的情况下,Map是在onResume()之后创建的。

此简化代码用于理解问题:

public class MapActivity{

    List<Templates> templates;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (app.isInit())) {
            initActivity();
        }

        templates.onCreate();
    }

    // init after templates are loaded
    private void initActivity() {
       templates = templateEngine.renderSomeTemplates()
    }

    @Override
    protected void onResume() {
        super.onResume();
        templates.onResume()
    }

    public void onEventMainThread(InitAppEvent event) {
       initActivity()
    }
}

所以我的问题

有没有办法在运行时以编程方式添加谷歌MapView而不传递onCreate()等等或者做一些懒惰的初始化?

2 个答案:

答案 0 :(得分:1)

好的,我明白了。

显然,如果没有文档中描述的回调,就无法生存: http://developer.android.com/reference/com/google/android/gms/maps/MapView.html

所以我发现了一个技巧,只要我的依赖项(模板)准备就绪,就可以强制我的活动重新开始。

如果有人正在寻找一种简洁的方法来重新启动活动:recreate() http://developer.android.com/reference/android/app/Activity.html#recreate()

它基本上完成了活动并从一开始就重复了正常的生命周期。

答案 1 :(得分:-1)

是的,可以查看SDK中的示例