I have loaded a huge layout XML ( > 1600 lines ) which has a view flipper with multiple layouts. It was first freezing at 'onCreate' but I changed to code to inflate the layout in background and pass the returned view to 'setContentView' at 'onPostExecute'. (This is working fine)
Now, when the activity is open (is in front) and the device is locked (or screen turned off), when I try to unlock the device, it freezes completely on some devices, and on other devices it shows a dialog saying "App isn't responding. Do you want to close it?" with options to "Wait" and "Ok".
Please help me to fix this issue.
Thanks
I finally figured out, what was causing the trouble.
I was using the LayoutInflater
in an AsyncTask
to inflate the whole XML into a View, like this...
@Override
protected View doInBackground(Void... params) {
Looper.prepare();
View view = getLayoutInflater().inflate(R.layout.my_large_xml, null, false);
return view;
}
@Override
protected void onPostExecute(Void result)
{
setContentView(view);
}
It turns out, that, when we set the content view using the view object, it freezes the device when activity is paused (I am not sure why).
But when I use the method setContentView(R.layout.my_large_xml)
, it works perfectly fine. So I dropped the idea of loading the XML in background, and now I am doing it on my main thread in the conventional way, because we cannot call setContentView
from another thread.
Sad ! Its not how I wanted it to be, as it freezes the app when the setContentView
is called initially, but at least its working now. :(
I will not give up, and keep trying. And, in case if I find any better solution, I will update the answer. :)