0

I have an app that opens to a screen with a couple of buttons. When I click on one button I start a new screen

   public void toGrid(View view) {
    Intent intent = new Intent(this, GridActivity.class);
    startActivity(intent);
  }

This second screen holds a bunch of running timers and Spinners that have certain values that can be selected.

My problem is that if I accidentally hit the back (not home) button on my phone I lose all my information from the secondary screen.

Is there a way that I can keep this information so that I would have to hit the back button on my phone from the main (initial) screen to lose the settings?

The other option would be to capture the "back" event from the secondary screen and put up some sort of "Are you sure" dialog.

1 Answers1

0

Is there a way that I can keep this information so that I would have to hit the back button on my phone from the main (initial) screen to lose the settings?

You can use SharedPreferences or SQLite database to store/clear this information. If you press the back button on your secondary, save all your data for each view like this

SharedPreferences.Editor editor_preferencias = getSharedPreferences("Preferencias", MODE_PRIVATE).edit();
editor_preferencias.putString("Data_Ultima_Visita_na_Agenda", myViewData);
editor_preferencias.commit();

Then if you open the secondary screen, pull back the data like this:

SharedPreferences preferencias = getSharedPreferences("Preferencias", MODE_PRIVATE);
String LastScheduleCheck = preferencias.getString("Data_Ultima_Visita_na_Agenda", null);

and finally, if you create the first activity again, just destroy the data setting it to null.

The other option would be to capture the "back" event from the secondary screen and put up some sort of "Are you sure" dialog.

That also works, as I've done it before but don't have the code on top of my head right now.

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
  • Looks like a good way to do it although i am not sure how to capture the phones back button click action. The second way to do it would probably be best as it means the timers keep running. – Brian Hewitson Apr 18 '15 at 06:19
  • http://stackoverflow.com/questions/2257963/how-to-show-a-dialog-to-confirm-that-the-user-wishes-to-exit-an-android-activity – Brian Hewitson Apr 18 '15 at 10:23