如何在Android生命周期阶段暂停直到调用回调? [英] How to pause at an Android lifecycle stage until callback is called?

查看:38
本文介绍了如何在Android生命周期阶段暂停直到调用回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Android应用中使用Google Maps API.我需要调用getMapAsync()以异步检索地图对象,并且需要在片段调用onResume()之前获取GoogleMap对象实例,因为否则,GoogleMap对象仍将为null,并且将引发异常.

I am trying to use Google Maps API in my Android app. I need to call getMapAsync() to retrieve the map object asynchronously and I need to get the GoogleMap object instance before my fragment calls onResume(), because otherwise the GoogleMap object will still be null and it will throw an exception.

因此,我希望保存该片段的Activity在onResume()处暂停,直到调用onMapReadyCallback为止,这样才能保证创建了GoogleMap对象,并且可以在Activity生命周期中继续进行.但是,如果我在Activity的线程(主线程)中使用任何锁或屏障同步器,则该线程将阻塞并且不会接收任何回调,因此将永远等待.

Therefore, I want the Activity that holds the fragment to pause at onResume() until onMapReadyCallback is invoked, so that I am guaranteed that the GoogleMap object is created and I could proceed in the Activity lifecycle. However, if I use any locks or barrier synchronizers in the Activity's thread (main thread), the thread will block and will not receive any callbacks and thus will wait forever.

我试图使用一个对象来实现onMapReadyCallback并将其放置在另一个线程中,但是根据文档,我必须在主线程中具有回调.

I tried to use an object to implement the onMapReadyCallback and put it in another thread, but according to the documentation, I must have the callback in the main thread.

所以我该如何使主线程等待直到回调到达,并防止其耗尽其生命周期方法??

So how can I make the main thread wait until the callback has arrived and prevent it from running down its lifecycle methods???

推荐答案

您有两种方法可以解决问题.一cr脚,一better脚.

You have two methods to resolve your issue. One crappy, and one better.

The脚的方式

例如,如果这对于测试目的有所帮助,则可以使用.当地图对象为null时,请在您的 onResume()方法中调用 Handler#postDelayed 方法.这是一个例子.

If this can helps for tests purposes for instance, this can works. Call the Handler#postDelayed method in your onResume() method when your map object is null. Here's an example.

@Override
public void onResume(){
    super.onResume();
    doMapStuff();
}

public void doMapStuff()
{
    if (mMap == null) {
        new Handler().postDelayed(new Runnable() {
            public void run() {
                doMapStuff();
            }
        }, 250);
        return;
    }

    // do your stuff on the map here.
}

public void onMapReady(GoogleMap map) {
    mMap = map;
    doMapStuff();
}

当在UI线程上创建处理程序时,延迟的runnable也将在UI线程上执行,因此您将能够制作地图内容.但是,这是一种糟糕的方法.它可以用于测试,但请避免使用这种方式!

As the handler is created on the UI thread, the delayed runnable will also be executed on the UI thread, so you will be able to make your map stuff. BUT This is a crappy way to do this. It can works for tests, but avoid using this way!

好方法

  • 如果在调用 onResume()时地图对象为null,则意味着将进一步调用 onMapReady 回调.
  • 如果在调用 onResume()时设置了地图对象,则意味着已经调用了 onMapReady 回调,因此不会再次调用.
  • If your map object is null when onResume() is called, that means the onMapReady callback will be called further.
  • If your map object is set when onResume() is called, that means the onMapReady callback was already called, so it will not be called again.

基本上,您需要在 onResume() onMapReady()方法之间复制"调用.

Basically, you need to "duplicate" your calls between onResume() and onMapReady() methods.

@Override
public void onResume(){
    super.onResume();
    doMapStuff();
}

public void doMapStuff()
{
    if (mMap == null)
        return;

    // do your stuff on the map here.
}

public void onMapReady(GoogleMap map) {
    mMap = map;
    doMapStuff();
}

这篇关于如何在Android生命周期阶段暂停直到调用回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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