views:

32

answers:

1

So I did the Notepadv1 tutorial. It worked great. No problems. I would however like some explanation on why the mNoteNumber remembers the last number of the item I created.

So the class starts as follows:

public class Notepadv1 extends ListActivity {
     private int mNoteNumber = 1;

That's fine, I understand that. The only other time the mNoteNumber variable is used is when you add an item it creates a note with that number and then increments it to the next number as follows:

private void createNote() {
 String noteName = "Note " + mNoteNumber++;

Those are the only two references to the variable mNoteNumber. When I press the Home button and then reopen the app, I add a new note but instead of adding a second "Note 1" it remembers that the last note I added as "Note 3" so it makes "Note 4". So I don't get it. Does Java/Android remember the last state of variables?

If anyone could give me some explanation that would be great THANKS!

+2  A: 

Hitting the home button does not kill your application. It simply moves it into the background. When you click on your application icon again, it moves the application back the foreground. Think about it like minimizing and reopening a window in an application.

However, you cannot rely on it remembering the state in this way. When an application is in the background it can be killed if Android decides it needs the space.

For more information, see the lifecycle documentation.

If you do want to guarantee that state is remembered you should persist the state as is explained in that document.

Mayra