我有这种代码,但似乎无法正常工作
public static class DeleteDir {
public static void main(String args[]) {
deleteDirectory(new File(args[0]));
}
static public boolean deleteDirectory(File path) {
if (path.exists()) {
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
deleteDirectory(files[i]);
} else {
files[i].delete();
}
}
}
return (path.delete());
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent intent = new Intent(getApplicationContext(),
TeacherSide.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
return super.onKeyDown(keyCode, event);
}
有人可以帮我解决这个问题吗?
答案 0 :(得分:4)
试试这个:
@Override
public void onBackPressed()
{
Intent intent = new Intent(getApplicationContext(),PreviousActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
finish();
}
如果是最后一个活动或者有任何PreviousActivity,那么请将此代码放在setContentView()
之前
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
setContentView(R.layout.main);
通过这种方式,您可以清除数据。
答案 1 :(得分:3)
在2.0之后我们有onBackPressed()
来处理后退按钮。
您可以覆盖onBackPressed()
功能并实现您的逻辑
@Override
public void onBackPressed()// called when the Back button is pressed
{
// do stuff
super.onBackPressed();
}
答案 2 :(得分:3)
对于使用onKeyDown
方法,这可能不是用于后退按钮的最佳方法,您的代码应该可以正常工作。我测试了你的代码,它确实对我有用。当按下后退按钮时,问题可能在于您运行的代码,而不是代码是否实际运行。
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent intent = new Intent(getApplicationContext(), TeacherSide.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// you don't need to call finish(); because
// return super.onKeyDown(keyCode, event); does that for you
// clear your SharedPreferences
getSharedPreferences("preferenceName",0).edit().clear().commit();
}
return super.onKeyDown(keyCode, event);
}
这是另一种方式:
使用Android的onBackPressed()
方法:
@Override
public void onBackPressed()
{
super.onBackPressed(); // this can go before or after your stuff below
// do your stuff when the back button is pressed
Intent intent = new Intent(getApplicationContext(), TeacherSide.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// super.onBackPressed(); calls finish(); for you
// clear your SharedPreferences
getSharedPreferences("preferenceName",0).edit().clear().commit();
}
答案 3 :(得分:1)
我建议您像其他人一样使用onBackPressed()
,除非您在API 5下创建某些内容,然后转到onKeyDown()
。
如果您真的想使用onKeyDown,请尝试使用此代码,并检查内部日志是否有效,然后将必要的代码放入
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.i("back press", "back");
return true;
} else {
Log.i("else back ", "else back");
}
return super.onKeyDown(keyCode, event);