As for rethinking the whole issue, the only thing I can think of is to get rid of the PreferenceActivity. I had the same problems a couple of months ago with the standard activity chooser for an Intent which is an Activity itself (never figured out why) so it caused spurious onPause()/onResume() on the main Activity. I ended up reimplementing it as a Dialog. :-( However I fear redoing PreferenceActivity as a Dialog might be a considerably bigger job.
I'd prefer not to do anything complex, precisely because as you mention, my situation is close to trivial - just two Activities, no fancy stack manipulations, no fancy nothing - it's the main Activity 99% of the time, with preferences coming up once in a while, just the main Activity onPause()/onResume() depends on the particular state transition, by nature of what it does. I must be missing something (like onPause()/onResume() on the whole application or something) - it can't be that Android application architecture is forced to fall apart into a bunch of Activities who can't coordinate among themselves efficiently... On Tue, Mar 5, 2013 at 1:59 PM, Piren <[email protected]> wrote: > I see. > > I can give you some pointers: > - This whole thing seems a bit convoluted, you might be better off taking > a 5 minute breather and instead of trying to fix the current issue, try to > figure out how to redesign your app so this issue never arrises... try to > attack it from a different angle. > - You dont need to monitor back presses... this is what > startActivityForResult is for, it lets you know you've returned from a > different activity that you started. > - there's no way to quickly "close" an android app other than backing up > from all the activities, so unless you've deliberately coded such a thing > (or used activity reordering techniques), you dont have to worry about that > happening. > - There are other ways to go back into Main other then using the Back > button.. but those are all ways you define - using StartActivity again for > Main (a flag like CLEAR_TOP and Main being SingleInstance would drop your > pref activity) so you dont have to worry about that. I did encounter some > issues in this area when it comes to launching using the icon, but thats > only if you have multiple entry paths into your app and you like to play > with activity flags :) > > > And for the last resort - it's something i've done myself but i'm not > quick to suggest it: Just keep track of activity lifecycles..I've had to do > it (for something much more complex than your situation) out as a last > option so i'm not fully endorsing it, but you can try it yourself. > > If you keep track of how many of your activities perform onStart/onStop (a > simple counter will do) you can easily see a pattern of when the app is in > the foreground or in the background, wether you're starting new activities > or them being taken down. then you should offload the job of deciding when > to do stuff to something that watches over the counters instead of just the > onPause of a specific activity. > > On Tuesday, March 5, 2013 2:03:29 PM UTC+2, latimerius wrote: > >> On Tue, Mar 5, 2013 at 12:13 PM, Piren <[email protected]> wrote: >> >>> The obvious way, since it's your app.. just keep a flag saying "im >>> starting an activity" whenever you start the prefs one... >>> >> >> Yes, that's what I'm doing at the moment. However, it's getting kludgy >> fairly fast. >> >> One problem is deciding what to do when now the PreferenceActivity gets >> onPause(). If it's because the user decided to leave the app (e.g. pressed >> Home while in prefs), the prefs Activity has to remember the main Activity >> has not actually paused properly (as it knew it was only getting onPause() >> to display prefs) and has to make sure to finish the pausing. However, if >> the prefs get onPause() because the user pushed Back to return to the main >> Activity, it doesn't have to do anything in particular. >> >> So now it's the prefs Activity who needs to understand why it's being >> paused. Except that now it's a bit harder. When the prefs Activity is >> being displayed, it's because the user pushed a button in the main >> Activity, so the main Activity knows for sure preferences are coming up. >> The PreferenceActivity has no such thing as a "Return to main Activity" >> button" as the only possible way the user could ever leave prefs and return >> to main. >> >> Sure, I can monitor Back presses - if Back was pushed before onPause() >> we're probably returning to the main Activity, if not we're probably >> leaving the app altogether. The keyword here being "probably" though as >> AFAIK this is not officially documented anywhere. So it smells like >> relying on undocumented behaviour (not that it matters much on Android if >> you rely on documented or undocumented ;-)). >> >> While trying to figure out whether Back is the only way of returning from >> prefs to main (and also, has it always been so? will it stay that way in >> the next Android version?) and googling for an answer I realised I'm not >> the first or only one who's trying to solve this. That's why I'm >> wondering, if there is a reasonably elegant and standard way of arranging >> things to solve these problems. >> >> -- > -- > 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 > --- > You received this message because you are subscribed to the Google Groups > "Android Developers" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- -- 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 --- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.

