0

When the users presses the back button in my application. Here is the scenario:

  1. User starts the application - Activity shows up
  2. User presses back button
  3. User re-starts the application. At this point application just shows a blank screen, none of the buttons(home/back) respond, after some time Force-close dialog comes up.
    NOTE: If the user presses "Home" and then relaunches the app, this doesn't happen, only if the user presses "Back" and then relaunches it.

In my onCreate() I have some network setup code. However, onDestroy() has the corresponding cleanup code, so I don't understand why this is happening.

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.d(DEBUG_TAG, "onCreate()");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

     // Aquire the multicast lock
     // Create an instance of JmDNS
     // Add a listener for Bonjour services of a given type 
}

@Override
protected void onDestroy() {
    Log.d(DEBUG_TAG, "onDestroy()");
     // Remove the services listener 
     // Set the reference to JmDNS instance null
     // Release the multicast lock
    super.onDestroy();
}

Not sure what is going on, and don't know how to debug this.

Interestingly - "Zeroconf Browser" a popular app that I downloaded from Android Market to use to debug mine - seems to have the same issue.

EDIT: Changed the code from onStart()/onStop() to onCreate()/onDestroy(). Same problem as before.

EDIT: For anyone who runs in a similar problem, this is what was causing my misery. Android code wasn't the culprit: http://sourceforge.net/tracker/index.php?func=detail&aid=2933183&group_id=93852&atid=605791

OceanBlue
  • 9,142
  • 21
  • 62
  • 84
  • 1
    when you press the back button the activity is destroyed, so the second time, it's in `onCreate()` that it gets stuck. What happens there? – bigstones Mar 30 '11 at 19:39
  • @Travis you could flag, but that's the default behavior for BACK button: http://developer.android.com/guide/practices/ui_guidelines/activity_task_design.html#navigating_away_from_an_activity – bigstones Mar 30 '11 at 20:01
  • @bigstones: +1. You are absolutely right. The activity **was** getting destroyed . That was what was causing the app to hang... there was a bug in my onDestroy() method of the activity. Thanks a TON! – OceanBlue Mar 30 '11 at 20:36
  • good to know that activities can look destroyed but actually be stuck in onDestroy(). – bigstones Mar 30 '11 at 20:38

1 Answers1

2

You may be making network requests on the UI thread. You might checkout Painless Threading and AsyncTask for handling that.

Note that it might be a good idea to do your setup and tear-down in onCreate and onDestroy. onStart can be called multiple times during the activity's life cycle; is your code guarding against this case?

Matthew
  • 44,826
  • 10
  • 98
  • 87
  • Thanks for your answer. Two points: 1. Indeed I am making network requests on the UI thread. Isn't that a problem, only if the network request is long one, like downloading a huge file etc.? 2. Actually my original code was in onCreate() & onDestroy(). I moved it to onStart()/onStop(), while debugging. The same problem is there if using onCreate()/onDestroy() too. – OceanBlue Mar 30 '11 at 19:50
  • Please see the EDIT on the my question. – OceanBlue Mar 30 '11 at 20:07
  • 1
    Can you use the debugger to get an idea of where your code is hanging? – Matthew Mar 30 '11 at 20:10
  • 1
    +1 I did just as you suggested. It was hanging on jmDNS.close() [which was a line in my cleanup code]. Looks like this is a bug in JmDNS, probably not Android. As bigstones very rightly pointed out above, the "BACK" button was executing onDestroy() which called the cleanup code, which hung in between. The "HOME" button did not call onDestroy(). So bug not getting recreated. Mystery solved.... Accepting your answer as it pointed me in the right direction & helped me solve it. – OceanBlue Mar 30 '11 at 20:33