我如果用戶不同意藍牙,它通過這段代碼要求的激活不應該打開一個片段如何等待onActivityResult完成?
Intent mIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
mIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 240);
startActivityForResult(mIntent, 1);
//Here I want to exit the method in case result is denied
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.content,new ServerFragment()).addToBackStack(null).commit();
我一直在尋找在相當多的地方,唯一的我發現的事情是使用新活動的setResult(在我的情況下,onActivityResult在調用活動中被覆蓋,因爲此代碼在一個片段內)
任何想法?
謝謝!
編輯:這裏有一個堆棧跟蹤
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=131073, result=240, data=null} to activity {bt.bt/bt.bt.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.ActivityThread.deliverResults(ActivityThread.java:3659)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3702)
at android.app.ActivityThread.access$1300(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
這是行不通的,因爲onActivityResult是異步的,這意味着調用片段不會等待結果,它會繼續執行。如果我嘗試從onActivityResult向屏幕添加新片段,則會生成IllegalStateException。 – Stefan
請發佈您的代碼和堆棧跟蹤。這是處理這些請求的標準方式。 – Francesc
發表了它,看一看 – Stefan