问题
When the users presses the back button in my application. Here is the scenario:
- User starts the application - Activity shows up
- User presses back button
- 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
回答1:
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?
来源:https://stackoverflow.com/questions/5491328/application-going-into-anr-mode