0

我如果用戶不同意藍牙,它通過這段代碼要求的激活不應該打開一個片段如何等待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 

回答

0

你應該只返回你的函數啓動意圖之後。然後等待onActivityResult中的回調。在這種方法中,如果結果是RESULT_OK,那麼你會開始你的片段。

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == your_request_code_here) { 
     if (resultCode != Activity.RESULT_CANCELED) { 
      // start fragment 
     } 
    } 
} 

全部活動:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent mIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); 
       mIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 240); 
       startActivityForResult(mIntent, 1); 
      } 
     }); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
    } 

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

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == 1) { 
      if (resultCode != Activity.RESULT_CANCELED) { 

      } 
     } 
    } 
} 
+0

這是行不通的,因爲onActivityResult是異步的,這意味着調用片段不會等待結果,它會繼續執行。如果我嘗試從onActivityResult向屏幕添加新片段,則會生成IllegalStateException。 – Stefan

+0

請發佈您的代碼和堆棧跟蹤。這是處理這些請求的標準方式。 – Francesc

+0

發表了它,看一看 – Stefan

0

正如你所粘貼代碼,您只要你叫startActivityForResult()實例的片段,你不需要做 - 這是造成問題。你只需要startActivityForResult(),然後在你的活動你onActivityResult()方法,你可以實例化你的片段,如果resultCode爲是RESULT_OK

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == your_request_code_here) { 
     if (resultCode == Activity.RESULT_OK) { 

getSupportFragmentManager().beginTransaction().replace(R.id.content,new ServerFragment()).addToBackStack(null).commit(); 
     } 
    } 
} 
+0

查看堆棧跟蹤 – Stefan

0

我認爲你應該使用廣播接收器註冊事件藍牙。 在你的Manifest.xml,你需要添加兩個聲明:

<uses-permission android:name="android.permission.BLUETOOTH" /> 
<receiver android:name=".BluetoothBroadcastReceiver" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" /> 
</intent-filter> 
    </receiver> 

然後,你需要實現的onReceive()方法:

public void onReceive(Context context, Intent intent) { 

    String action = intent.getAction(); 
    Log.d("BroadcastActions", "Action "+action+"received"); 
    int state; 
    BluetoothDevice bluetoothDevice; 

    switch(action) 
    { 
     case BluetoothAdapter.ACTION_STATE_CHANGED: 
      state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); 
      if (state == BluetoothAdapter.STATE_OFF) 
      { 
       Toast.makeText(context, "Bluetooth is off", Toast.LENGTH_SHORT).show(); 
       Log.d("BroadcastActions", "Bluetooth is off"); 
      } 
      else if (state == BluetoothAdapter.STATE_TURNING_OFF) 
      { 
       Toast.makeText(context, "Bluetooth is turning off", Toast.LENGTH_SHORT).show(); 
       Log.d("BroadcastActions", "Bluetooth is turning off"); 
      } 
      else if(state == BluetoothAdapter.STATE_ON) 
      { 
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.content,new ServerFragment()).addToBackStack(null).commit(); 
       Log.d("BroadcastActions", "Bluetooth is on"); 
      } 
      break; 

.... 
} 

你可以看到更多的在這個環節:Android Broadcast Receiver bluetooth events catching