Application going into ANR mode

徘徊边缘 提交于 2019-12-23 04:40:26

问题


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


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!