如何在onStart()内完成方法A()之后在onResume()内启动方法B()

问题描述 投票:0回答:2

如何在onStart()内完成方法A()(工作线程)之后在onResume()内启动方法B()(主线程)

我在onStart()中有loadCards(),在onResume()里面有initViews()。

只有在loadCards()完成后才需要运行initViews(),loadCards()是一个长时间运行的操作,并且有一个回调()。

当前的问题是initViews()在loadCards()完成之前运行,因此得到一个空指针。

想获得帮助:如何在loadCards()(onStart内部)完成后运行initViews()(在onResume内)?

@Override
protected void onStart() {
    super.onStart();

    loadCards(new Callback(){
        success(List<Card> cardList){do something}
        fail(){do other thing}
    });
}

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

    //my problem is here: 
    //how to know if loadCards() has finished? then run initViews.

    initViews();
}

public void loadCards(Callback callback) {
    Runnable runnable = () -> {

        //List<Card> cardList = get list from work thread;

        mAppExecutors.mainThread().execute(() -> {
            if (result == empty) {
                callback.fail();
            } else {
                callback.success(cardList);
            }
        });
    };

    mAppExecutors.diskIO().execute(runnable);
}

void initViews(){}

预期:在loadCards()完成后,运行initViews()。

实际:当loadCards()仍在运行时,initViews()运行。

android mvp lifecycle
2个回答
1
投票

把你的initViews();放在success里面就像这样

@Override
protected void onStart() {
    super.onStart();

    loadCards(new Callback(){
        success(List<Card> cardList){initViews();}
        fail(){do other thing}
    });
}

0
投票

您可以使用https://github.com/EliyahuShwartz/ConditionalTasksRunner在满足特定条件时运行特定任务(例如,当恢复活动时)

此代码是为从另一个类(通常是演示者)运行视图任务而设计的,而不是硬件到android生命周期。

基本的实施将是

   private val mResumePendingTasks: ConditionalTasksRunner = object : ConditionalTasksRunner() {
        override val isConditionMet = isResumed
    }

然后你就可以在decleration类中的任何地方发布任务。

mResumePendingTasks.runOnConditionMet(Runnable { onListLoadedSuccessfully() })

ConditionalTask​​sRunner的源代码是:

package conditionaltasksrunner.com

import android.os.Handler
import android.os.Looper
import java.util.*

/**
 * Run tasks when a specific condition is met [.isConditionMet]
 * this tasks will run on the [Handler] that create the constructor
 */
abstract class ConditionalTasksRunner {

    private var mPendingTask: MutableSet<Runnable>
    private var mHandler: Handler

    /**
     * @return true if condition is met
     */
    abstract val isConditionMet: Boolean

    protected constructor() {
        mPendingTask = LinkedHashSet()
        mHandler = Handler()
    }

    protected constructor(handler: Handler) {
        mPendingTask = LinkedHashSet()
        mHandler = handler
    }

    /**
     * Run the given task when condition is met.
     * if the condition is met already than the task will invoke immediately
     *
     * @param runnable task to run
     */
    @Synchronized
    fun runOnConditionMet(runnable: Runnable) {
        if (isConditionMet) {
            if (Looper.myLooper() == mHandler.looper)
                runnable.run()
            else
                mHandler.post { runOnConditionMet(runnable) }
        } else {
            mPendingTask.add(runnable)
        }
    }

    /**
     * Remove the task from the pending task queue
     *
     * @param runnable the task to remove
     * @return true if the task was removed successfully
     */
    @Synchronized
    fun remove(runnable: Runnable): Boolean {
        mHandler.removeCallbacks(runnable)
        return mPendingTask.remove(runnable)
    }

    /**
     * Should be called when the condition is met
     */
    @Synchronized
    fun onConditionMet() {
        if (Looper.myLooper() == mHandler.looper) {
            val iterator = mPendingTask.iterator()

            while (iterator.hasNext()) {
                iterator.next().run()
                iterator.remove()
            }
        } else {
            mHandler.post { this.onConditionMet() }
        }
    }

    /**
     * Clear all the pending tasks and remove them from the handler queue
     */
    @Synchronized
    fun clear() {
        for (runnable in mPendingTask)
            mHandler.removeCallbacks(runnable)

        mPendingTask.clear()
    }
}

请查看完整示例的源代码。

© www.soinside.com 2019 - 2024. All rights reserved.