Question

In my application, on every start of the application I want to execute a php script and load the application accordingly. Hence I created a static boolean varialble

boolean firstRun = false;

In my onStart, I add teh following code :

// If images need to be downloaded, call SProgressAsyncTask class
    public void onStart() {
    super.onStart();

    if (firstRun) {
        sat = new SProgressAsyncTask(this);
        sat.execute("");

        firstRun = false;
    }
}

On a friend'd device of Sony, it works perfectly i.e. the above code executes only once i.e. start of the application. On my Sony Xperia P, the above code executes everytime the application comes to this main screen. When the firstRun is made false, then why is the above code running everytime it comes back to this activity. I tried debugging on my device and I get the following error :

10-22 17:41:49.879: E/WindowManager(30235): Activity org.mumbai77.core.Mumbai77Activity has    leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4143a460 that was originally added here 
10-22 17:41:49.879: E/WindowManager(30235): android.view.WindowLeaked: Activity org.mumbai77.core.Mumbai77Activity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4143a460 that was originally added here 
10-22 17:41:49.879: E/WindowManager(30235):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
10-22 17:41:49.879: E/WindowManager(30235):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
10-22 17:41:49.879: E/WindowManager(30235):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
10-22 17:41:49.879: E/WindowManager(30235):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
10-22 17:41:49.879: E/WindowManager(30235):     at android.view.Window$LocalWindowManager.addView(Window.java:537)
10-22 17:41:49.879: E/WindowManager(30235):     at android.app.Dialog.show(Dialog.java:278)
10-22 17:41:49.879: E/WindowManager(30235):     at org.mumbai77.components.ProgressAsyncTask.onPreExecute(ProgressAsyncTask.java:56)
10-22 17:41:49.879: E/WindowManager(30235):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
10-22 17:41:49.879: E/WindowManager(30235):     at android.os.AsyncTask.execute(AsyncTask.java:511)
10-22 17:41:49.879: E/WindowManager(30235):     at org.mumbai77.core.Mumbai77Activity.onStart(Mumbai77Activity.java:170)
10-22 17:41:49.879: E/WindowManager(30235):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
10-22 17:41:49.879: E/WindowManager(30235):     at android.app.Activity.performStart(Activity.java:4475)
 10-22 17:41:49.879: E/WindowManager(30235):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1940)
10-22 17:41:49.879: E/WindowManager(30235):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
10-22 17:41:49.879: E/WindowManager(30235):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-22 17:41:49.879: E/WindowManager(30235):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
10-22 17:41:49.879: E/WindowManager(30235):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 17:41:49.879: E/WindowManager(30235):     at android.os.Looper.loop(Looper.java:137)
10-22 17:41:49.879: E/WindowManager(30235):     at android.app.ActivityThread.main(ActivityThread.java:4441)
10-22 17:41:49.879: E/WindowManager(30235):     at java.lang.reflect.Method.invokeNative(Native Method)
10-22 17:41:49.879: E/WindowManager(30235):     at java.lang.reflect.Method.invoke(Method.java:511)
10-22 17:41:49.879: E/WindowManager(30235):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-22 17:41:49.879: E/WindowManager(30235):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-22 17:41:49.879: E/WindowManager(30235):     at dalvik.system.NativeStart.main(Native Method)

I changed the SProgressAsyncTask class and instead of passing Activity passed Context - getApplicationContext(), but that made it more worse.

Can anyone help me know, why the onStart if (firstRun) is executing each time rather than just executing once.

BTW, mine & my firend's device both have ICECREAM Os only.

Am stuck at this stage fro mlast 5-6 days, any help is highly appreciative. Please help me get rid of this error.

Was it helpful?

Solution 3

How unexpectedlt this worked !

I just tried in a static class created a variable

public static boolean FIRST_START = true;

and in my activity class, replaced firstRun varialbe with CONSTANTS.FIRST_START and also set CONTACTS.FIRST_START = false inside the if of onStart(). And now it calls the method only once i.e. while actually start of the app. Then again when I come back to the activity it is not called.

Same method, just used different var of different class. How strange is this ? If at all, this may help someone.

OTHER TIPS

I assume that you set the firstRun object to true in the onCreate else it will never start.

If you do this, you should check the activity lifetime again, the activity starts with an onCreate (where you set firstRun to true) then the onStart comes.

With this setup, the app will launch this ASyncTask on each startup

This exception I have also faced might be you are caught in the same situation. AsyncTask works in background and an exception occurs in parallel executing UI thread. AsyncTask is still running trying to show dialog which gives this exception. The exact cause is also get printed in logcat just before this exception stack trace. Check your logcat.

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