That's a creative way to work with Android lifecycle management. In
fact you don't need to tell Android to go to the Home page when the
user presses the back button. If you did nothing special when the user
pressed the back button, Android would call onPause() and go to the
previous Activity.

Activities are like a stack. So when you call startActivity(), you are
telling Android "put this activity on top of mine". Instead, just wait
for Android to tell your Edit activity that it's about to go away
(from the back button which pops one activity off the stack, or the
home button which pops them all, or other causes), and save your data
in onPause(). You can then reload it in onResume() when Android tells
your activity to start back up. Incidentally, Android also calls
onResume() the very first time your activity is launched.

In case you've not seen the activity lifecycle description, here it
is: http://developer.android.com/guide/topics/fundamentals.html#actlife



Yusuf Saib
Android
·T· · ·Mobile· stick together
The views, opinions and statements in this email are those of the
author solely in their individual capacity, and do not necessarily
represent those of T-Mobile USA, Inc.




On Jul 24, 5:23 am, Anna PS <[email protected]> wrote:
> Hi all,
>
> This is an intermittent bug in one of my apps. I don't expect anyone
> to solve it immediately, but I'd appreciate any thoughts on how to
> start debugging!
>
> So, my app lets users edit an item (in the EditPage activity). When
> they press the Back key, the desired behaviour is that it saves their
> edits and returns them to the home page (the HomeList activity). The
> code for this is below. It works fine. Except...
>
> The bug is this. Some users report that sometimes when they reopen the
> application after a pause, they are seeing *not* the home page, where
> they left the application - but the previous edit activity. Crucially,
> they are also seeing the edit activity without their saved edits -
> effectively losing their most recent edits :(
>
> I've been able to reproduce this myself. It only happens after the
> application hasn't been opened for a while. If I save something,
> minimize the application and reopen it straight away, it shows the
> HomeList activity just fine, and the edits have all been saved. But if
> I save something, leave it for a couple of hours and then reopen it,
> it shows the previous EditPage activity, without the saved edits (i.e.
> in the state that the EditPage activity was previously launched).
>
> Any ideas, anyone? I had wondered whether it was due to the Back
> button being used to return the user to the HomeList activity - maybe
> Android thinks that the Back button means that the HomeList is not the
> most "recent" activity, instead the EditPage is. I'd like to carry on
> using the Back button if possible, though. And in particular, I'm
> puzzled why it only happens after the app has been idle for an hour or
> so, and not immediately? Is it something to do with Android's memory
> management?
>
> As I say, would appreciate any tips on how to debug - quite baffled at
> the moment!
>
> Thanks very much,
>
> Anna
>
> ----------
>
> Code used in the EditPage activity to save edits when the Back key
> gets pressed. Works fine, apart from bug described above.
>
>         // Save everything when the Back key gets pressed
>         public boolean onKeyDown(int keyCode, KeyEvent event) {
>                 if (keyCode == KeyEvent.KEYCODE_BACK) {
>                         if (saveEdits()) {
>                                 Context context = getApplicationContext();
>                                 CharSequence text = "'" + title + "' has been 
> saved";
>                                 int duration = Toast.LENGTH_SHORT;
>                                 Toast toast = Toast.makeText(context, text, 
> duration);
>                                 toast.show();
>                                 Intent i = new Intent(EditPage.this, 
> HomeList.class);
>                                 startActivity(i);
>                         } else {
>                                 // failed to save!
>                                 showDialog(SAVE_WARNING);
>                         }
>                 }
>                 return false;
>         }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to