从后台线程完成android活动是否安全? [英] Is it safe to finish an android activity from a background thread?

查看:84
本文介绍了从后台线程完成android活动是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中,从后台线程调用Activity.finish()是安全的,还是只能从主线程调用? 文档没有提及任何有关线程的内容这种方法的安全性.

In Android, is it safe to call Activity.finish() from a background thread, or can it only called from the main thread? The documentation doesn't mention anything about thread safety of this method.

推荐答案

不,不是.

代码使用至少一个mFinished变量,而不进行同步.句号.

The code uses at least one variable, mFinished, without synchronization. Full stop.

   public void finish() {
    if (mParent == null) {
        int resultCode;
        Intent resultData;
        synchronized (this) {
            resultCode = mResultCode;
            resultData = mResultData;
        }
        if (false) Log.v(TAG, "Finishing self: token=" + mToken);
        try {
            if (resultData != null) {
                resultData.setAllowFds(false);
            }
            if (ActivityManagerNative.getDefault()
                .finishActivity(mToken, resultCode, resultData)) {
                mFinished = true;
            }
        } catch (RemoteException e) {
            // Empty
        }
    } else {
        mParent.finishFromChild(this);
    }
}

这篇关于从后台线程完成android活动是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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