I want that when the user is pressing the back button on the keyboard of the android, the activity will close (and the preview activity will be shown) and beside that i would like the app to do another thing.
When I tried to do by the Instructions here:
is there a default back key(on device) listener in android? when I'm pressing the back button, the activity is close and open again. When I'm pressing the back button again the activity is close and the preview activity is opend.
Anybody knows why? Does anybody have another idea for this?
This is some of the codes i tried: Option One:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
//Show the dialog and get the response
ArrayList<User> lstChosenContacts = new ArrayList<User>();
for(int i = 0; i < this.lstContacts.size(); i++) {
if(this.lstContacts.get(i).getIsChecked()) {
lstChosenContacts.add(this.lstContacts.get(i).getUserContact());
}
}
Intent data = new Intent();
data.putExtra("lstChosenContacts", lstChosenContacts);
setResult(RESULT_OK,data);
}
return super.onKeyDown(keyCode, event);
}
Option 2:
@Override
public void onBackPressed() {
ArrayList<User> lstChosenContacts = new ArrayList<User>();
for(int i = 0; i < this.lstContacts.size(); i++) {
if(this.lstContacts.get(i).getIsChecked()) {
lstChosenContacts.add(this.lstContacts.get(i).getUserContact());
}
}
Intent data = new Intent();
data.putExtra("lstChosenContacts", lstChosenContacts);
setResult(RESULT_OK,data);
super.onBackPressed();
}
Thanks!