![](/img/trans.png)
[英]How can I stop asynctask in android when back button is pressed?
[英]how can I clear data when device back button is pressed?
我有這種代碼,但它似乎不起作用
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();
}
如果這個最后一個活動或者有任何PreviousActivity,那么將此代碼放在setContentView()
之前
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
setContentView(R.layout.main);
通過這種方式,您可以清除數據。
在2.0之后我們有onBackPressed()
來處理Back按鈕。
您可以覆蓋onBackPressed()
函數並實現您的邏輯
@Override
public void onBackPressed()// called when the Back button is pressed
{
// do stuff
super.onBackPressed();
}
對於使用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();
}
我建議你像其他人一樣使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.