gpt4 book ai didi

android - 按下设备后退按钮时如何清除数据?

转载 作者:太空狗 更新时间:2023-10-29 15:38:33 25 4
gpt4 key购买 nike

我有这种代码,但它似乎不起作用

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);
}

谁能帮我解决这个问题?

最佳答案

试试这个:

@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();
}

如果这是最后一个 Activity 或有任何 PreviousActivity ,则将此代码放在 setContentView()

之前
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
setContentView(R.layout.main);

通过这种方式可以清除数据。

关于android - 按下设备后退按钮时如何清除数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20917669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com