Is it safe to shut down an android from a background thread?

On Android, is it safe to call Activity.finish() from a background thread, or is it called only from the main thread? The documentation says nothing about the thread safety of this method.

+7
android android-activity thread-safety
source share
2 answers

No, it is not.

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); } } 
+12
source share

Probably the result may be unexpected. I would use a handler or some other way to put an order into action to kill myself in the next iteration.

0
source share

All Articles