Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - problems with a multi-level activity chain

(Note that I've searched online for the warnings I'm describing below, and have come up with next to nothing about them.)

I'm working with API level 10. I have a preference screen (XML-based), and one of the options in there creates a custom ListActivity as follows:

  • PreferenceActivity contains an option that creates a...
    • ListActivity which is a dialog that employs...
      • setOnClickListener() which contains an onClick() method that (right before calling finish()) will startActivity() a new Intent...
        • sub-Activity which starts up an...
          • AsyncTask which does variable time work which when done calls...
          • onPostExecute() which calls finish()

The thing is, it works... but I'm getting a raft of warning starting with:

10-16 21:59:25.010: WARN/WindowManager(170): Rebuild removed 4 windows but added 3
10-16 21:59:25.010: WARN/WindowManager(170): This window was lost:.....

Curiously, this raft of warnings ONLY comes up when the task executes quickly! When I added a Thread.sleep() call to my AsyncTask to artificially inflate its runtime it worked and threw no warnings whatsoever. In fact, as long as it takes more than (roughly) 500 ms to run it works fine. (Note that I tried using startActivityForResult() to no greater effect - the same problem occurs.)


The goal is that the user selects a preference item, they change its setting, some processing takes place, and then the user is left back at the preference menu they started on.

I'm betting it's a race condition... the order in which the windows are destroyed varies depending on that run-time... and I get the impression that when the sub-Activity closes before its parent ListActivity the warnings get thrown. But sprinkling a 1s sleep() in isn't a reasonable solution unless this is some sort of Android bug (unlikely, but then again I've reproduced a couple of those today already).

So, what's the flaw in this my that leads to this stream of warnings? It'd be nice to say "on preference, do this, then do that, then finish" but I think what I'm doing is the equivalent. Maybe not... thoughts?


Edit: I decided to try doing this ListActivity as a custom Dialog... that was one of the more painful things I've tried to do lately (getApplication() doesn't work and lots of other things seem to go wrong... it may be inexperience to some extent, but dialogs really weren't meant for this either...

like image 386
MartyMacGyver Avatar asked Oct 17 '11 04:10

MartyMacGyver


1 Answers

Try the following two things:

  • Dismiss your dialog before calling finish() on its parent activity (PreferenceActivity).

  • Make sure you are starting your AsyncTask later in the sub-activity's lifecycle. I'm specifically thinking you should launch it in onResume().

My best guess is that the AsyncTask is calling finish() on the sub-activity, before the sub-activity has had a chance to fully start up. Why that would matter? I'm not sure. Something to try though. Good luck!

like image 175
Eric Schlenz Avatar answered Oct 19 '22 11:10

Eric Schlenz