Reputation: 69
I have this kind of code but it seems not to work
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);
}
Can anyone please help me with this?
Upvotes: 2
Views: 5867
Reputation: 13483
For using the onKeyDown
method, which is probably not the best method to use for the back button, your code should work. I tested your code and it did work for me. The problem may lie in the code you run when the back button is pressed, not with whether or not the code actually runs.
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);
}
Here's another way:
Use Android's onBackPressed()
method:
@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();
}
Upvotes: 3
Reputation: 2879
Try this:
@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();
}
And if this last activity or there is any PreviousActivity , then put this code before setContentView()
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
setContentView(R.layout.main);
In this way you can clear data.
Upvotes: 4
Reputation: 1423
I suggest you using onBackPressed()
like other people say unless you creating something below API 5 then go for onKeyDown()
.
If you really want to use the onKeyDown try this code and check inside log if it work then put necessary code inside
@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);
Upvotes: 1
Reputation: 10563
After 2.0 we have onBackPressed()
to handle Back button.
You can override onBackPressed()
function and implement your logic
@Override
public void onBackPressed()// called when the Back button is pressed
{
// do stuff
super.onBackPressed();
}
Upvotes: 3