1

我为我的活动实现了 onBackPressed,它将检查互联网连接,但是当我单击平板电脑中的后退按钮时,它什么也不做。我不明白这是什么原因。可以帮忙?

下面是我的代码

if (!cd.isConnectingToInternet()) {
        AlertDialog.Builder splash = new AlertDialog.Builder(this);
        splash.setIcon(R.drawable.ic_fail)
                .setTitle("No Internet Connection")
                .setMessage(
                        "Please check your internet connection and try again.")
                .setCancelable(false)
                .setPositiveButton("Try again",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                Intent splash = new Intent(
                                        getApplicationContext(),
                                        SplashActivity.class);
                                startActivity(splash);
                                finish();
                            }
                        })
                .setNegativeButton("Wifi Setting",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                startActivity(new Intent(
                                        android.provider.Settings.ACTION_WIFI_SETTINGS));
                                dialog.cancel();
                            }
                        });
        AlertDialog alert = splash.create();
        alert.show();
    } else {
        Thread timer = new Thread() {
            public void run() {
                try {
                    sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    Intent login = new Intent(getApplicationContext(),
                            LoginActivity.class);
                    startActivity(login);
                    finish();
                }
            }
        };
        timer.start();
    }
}

public void onRestart() {
    super.onRestart();
    Intent splash = new Intent(getApplicationContext(),
            SplashActivity.class);
    startActivity(splash);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}
4

5 回答 5

4

试试这个代码......

 @Override
        public void onBackPressed()
        {
            moveTaskToBack(true);
        }
于 2012-11-30T10:21:52.910 回答
0

尝试这个:

@Override
    public void onBackPressed() {
        yourclassname.this.finish();
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
        getParent().finish();
    }
于 2012-11-30T07:09:40.437 回答
0

- finish()是关闭活动的正确方法。

-但是如果它没有,由于某种原因System.exit(0)finish()..之后使用它肯定会起作用....我知道它很粗糙......但是有效......

/////////////////////////////////////////////////////////////////////////// ///////////////////

-覆盖的方法onKeyDown()Activity

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 

               this.finish(); 

            } 
        return true;
    }
于 2012-11-30T06:50:20.480 回答
0

评论,并检查

//super.onBackPressed();
于 2012-11-30T06:50:33.507 回答
0

试试这个。我认为它会为你工作。

@Override
public void onBackPressed() {
//super.onBackPressed();
// finish your Activity
ActivityName.this.finish();
return;
}
于 2012-11-30T07:16:13.280 回答