0

So what I'm trying to do is make it so the user is not able to go back to the previous activity without clicking the "save" button. I just don't know how to implement a back button exception sort of thing. Why I'm wanting to do this is because when they click the back button the edit text in which they are inputting information is still being saved to the sql database. Here is my code for the button.

saveItemButton.setOnClickListener(new View.OnClickListener() {
  public void onClick(View view) {
    if (isEmpty(mTitleText)) {
        mTitleText.getText().toString();
      makeToast();
    } else {
      setResult(RESULT_OK);
      finish();
    }
  }
});
Fernando
  • 450
  • 1
  • 7
  • 22
  • I think you should reconsider your approach. Why don't you save to the database only after the user presses the save button? This way if the user presses back, you just cancel the insertion. You can't really prevent the user from leaving your activity without saving if he/she wants to. – Ricardo Nov 24 '13 at 17:45
  • Thing is I'm working off a tutorial database, since I'm really new to databases and this one just saves everything as you type it, it's a bug they had that I figured out. I just can't seem to fix it. @Ricardo – Fernando Nov 24 '13 at 17:48
  • Can you put the code for the saving part here? – Ricardo Nov 24 '13 at 17:49
  • @Ricardo http://pastebin.com/FMrxqE1k – Fernando Nov 24 '13 at 17:52
  • Why don't you move the saveState() call to the onClick() of a save button? – Ricardo Nov 24 '13 at 17:57
  • I had to comment some stuff out and modify a bit but that saveState method did it. Thanks a lot, that bug pretty much made my app useless and my in tears. haha. Thanks again @Ricardo – Fernando Nov 24 '13 at 18:02

2 Answers2

0

The user can always leave by pressing the Home button. There isn't anything you can do about that. If you still want to disable the back button, override onBackPressed() in your activity and don't call super.onBackPressed()

dandc87
  • 1,096
  • 5
  • 7
0

you should override the onBackPressed

@override public void onBackPressed(){

//here your

}

saddam
  • 11
  • 2