Question

According to an answer by CommonsWare:

onResume() is called any time an activity is regaining the foreground input. This includes:

  • When it is returning to the screen after something else had the foreground (e.g., Settings), and

  • When it is being created for the first time in this process (which includes any new process required because you killed the old one from DDMS)

Hence, your code will examine the state of the ringer mode in either case and will use the proper image in either case.

But in my case, onResume() is being called while the app is running without any of the above conditions. onResume() has a refresh() method which shows a ProgressBar, loads some contents in the WebView, and then hides the ProgressBar and shows the WebView.

Note that it doesn't always happen, but most of the times it does.

I'm really curious to know why this happens, and if possible, how could I prevent it because it is really annoying for the users to see it happen.

Code:

public void refresh()
{
        webView.clearCache(true);
        layout.setVisibility(View.GONE);
        progress.setVisibility(View.VISIBLE);
        fetcher = new DataFetcher();

        String url = "http...." // some url
        fetcher.execute(url);
}

public void onResume()
{
        super.onResume();
        refresh();
}

EDIT Here's what I got after Thread.dumpStack() (these show twice in logcat, so onResume() is called twice):

12-12 10:36:22.033: W/HardwareRenderer(22358): Attempting to initialize hardware acceleration outside of the main thread, aborting
12-12 10:36:24.947: W/HardwareRenderer(22358): Attempting to initialize hardware acceleration outside of the main thread, aborting
12-12 10:36:26.744: W/HardwareRenderer(22358): Attempting to initialize hardware acceleration outside of the main thread, aborting
12-12 10:36:27.064: W/System.err(22358): java.lang.Throwable: stack dump
12-12 10:36:27.088: W/System.err(22358):    at java.lang.Thread.dumpStack(Thread.java:496)
12-12 10:36:27.088: W/System.err(22358):    at com.example.app.menufragments.SalesByPointsFragment.onResume(SampleFragment.java:101)
12-12 10:36:27.088: W/System.err(22358):    at android.support.v4.app.Fragment.performResume(Fragment.java:1543)
12-12 10:36:27.088: W/System.err(22358):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:963)
12-12 10:36:27.088: W/System.err(22358):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-12 10:36:27.088: W/System.err(22358):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
12-12 10:36:27.088: W/System.err(22358):    at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1894)
12-12 10:36:27.088: W/System.err(22358):    at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:466)
12-12 10:36:27.088: W/System.err(22358):    at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:455)
12-12 10:36:27.088: W/System.err(22358):    at com.actionbarsherlock.app.SherlockFragmentActivity.onPostResume(SherlockFragmentActivity.java:68)
12-12 10:36:27.088: W/System.err(22358):    at android.app.Activity.performResume(Activity.java:5195)
12-12 10:36:27.088: W/System.err(22358):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2860)
12-12 10:36:27.088: W/System.err(22358):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2899)
12-12 10:36:27.088: W/System.err(22358):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
12-12 10:36:27.088: W/System.err(22358):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 10:36:27.088: W/System.err(22358):    at android.os.Looper.loop(Looper.java:137)
12-12 10:36:27.088: W/System.err(22358):    at android.app.ActivityThread.main(ActivityThread.java:5227)
12-12 10:36:27.096: W/System.err(22358):    at java.lang.reflect.Method.invokeNative(Native Method)
12-12 10:36:27.096: W/System.err(22358):    at java.lang.reflect.Method.invoke(Method.java:511)
12-12 10:36:27.096: W/System.err(22358):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
12-12 10:36:27.096: W/System.err(22358):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
12-12 10:36:27.096: W/System.err(22358):    at dalvik.system.NativeStart.main(Native Method)

I also put Thread.dumpStack() at onPause(), and again the logs start with:

12-12 10:55:38.502: W/HardwareRenderer(29996): Attempting to initialize hardware acceleration outside of the main thread, aborting
12-12 10:55:38.572: W/HardwareRenderer(29996): Attempting to initialize hardware acceleration outside of the main thread, aborting
12-12 10:55:46.369: W/HardwareRenderer(29996): Attempting to initialize hardware acceleration outside of the main thread, aborting
12-12 10:55:50.736: W/HardwareRenderer(29996): Attempting to initialize hardware acceleration outside of the main thread, aborting

I believe this has to do with the WebView, perhaps the intensive work makes it pause and resume?


Also I get warnings of Choreographer. It all happens because of the WebView I believe as it shows javascript charts. Does the WebView do it's processing in the main thread?

Was it helpful?

Solution 2

Just a small update. Although the activity lifecycle states otherwise, it was calling onResume twice. But it happened only when debugging and not normally. So it probably was a bug.

OTHER TIPS

According to the activity lifecycle, it is not possible that it does onResume() once it's running, unless it does first onPause().

enter image description here

Are you pausing the app anyway? Maybe turning off the screen (even automatically)? Or changing between activities? If not, I'd check if you change the ProgressBar and WebView in other parts of your code, it seems the problem is not onResume.

You can always write a Log.i(tag, string) and see when it goes to the method.

I wanted to comment instead answering, but I have not enough reputation, sorry.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top