当我取消或接受蓝牙启用请求时,我的活动会关闭吗?

时间:2013-05-02 10:31:10

标签: android start-activity

只有当前活动关闭且之前的活动打开,logcat中没有错误。

以下是puttin bluetooth enable request的代码:

 bt.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
               if (!(bluetooth.isEnabled())) {
                   Log.e("blue",""+"not working");
                   status = "Bluetooth is not Enabled.";
                   Toast.makeText(AddUser.this, status, Toast.LENGTH_SHORT).show();
                   Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, 1);

                }
                else
                {

                    scand();
                }

           }



    });

这是onactivityresult:

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    System.out.println(resultCode);
    Log.e("resultblue",""+resultCode);
    if (resultCode ==  RESULT_CANCELED) {


        runOnUiThread(new Runnable() {
            public void run() {

             Toast.makeText(AddUser.this, "Error Enabling bluetooth", Toast.LENGTH_LONG).show();
            }
        });
    } else {

            scand();
    }



}

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

I had simmilar problem.

please remove no history from manifest.

<activity
android:name=".ActivityName"
android:noHistory="true" />

if you want avoid coming back to previous activity - preloader for example you can use something like this:

private boolean doubleBackToExitPressedOnce = false;



 @Override
    public void onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            finishAffinity();
            finish();
            android.os.Process.killProcess(android.os.Process.myPid());
            return;
        }

        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, R.string.press_again_to_exit, Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                doubleBackToExitPressedOnce = false;
            }
        }, 2000);
    }

Handler is needed for reset doubleBackToExitPressedOnce variable.