garretyoder / Colorful

Android runtime theme library
Apache License 2.0
2.13k stars 193 forks source link

Performing pause of activity that is not resumed #7

Closed DanteAndroid closed 6 years ago

DanteAndroid commented 7 years ago

When I apply theme in my setting activity, return to MainActivity, then go to setting activity, I got the following error log(which seems not influence my app):

12-28 17:37:49.563 23151-23151/com.dante.girls E/ActivityThread: Performing pause of activity that is not resumed: {com.dante.girls/com.dante.girls.MainActivity}
                                                                 java.lang.RuntimeException: Performing pause of activity that is not resumed: {com.dante.girls/com.dante.girls.MainActivity}
                                                                     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3352)
                                                                     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3340)
                                                                     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3315)
                                                                     at android.app.ActivityThread.-wrap13(ActivityThread.java)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Each time I do those actions I got that error, so it's not occasional.

meierjan commented 7 years ago

Performing pause of activity that is not resumed

Can you link the version of your MainActivity here? I see your project is open source

DanteAndroid commented 7 years ago

You mean this? Main extends it.

Elvis10ten commented 7 years ago

I experience the same issue. I believe the problem is cause by the recreate call in onResume of ColorfulActivity. The solution is to slightly delay the recreate call using a handler. Found this answer on stackoverflow, might help: http://stackoverflow.com/questions/10844112/runtimeexception-performing-pause-of-activity-that-is-not-resumed

Elvis10ten commented 7 years ago

This fixed it for me

`import android.app.ActivityManager.TaskDescription;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import org.polaric.colorful.Colorful;

public abstract class ColorfulActivity extends AppCompatActivity {

    private static final String LOG_TAG = "ColorfulActivity";

    private String mThemeString;

    public ColorfulActivity() {
    }

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mThemeString = Colorful.getThemeString();
        setTheme(Colorful.getThemeDelegate().getStyle());

        if(VersionUtils.hasLollipop()) {
            if(Colorful.getThemeDelegate().isTranslucent()) {
                LogUtils.d(LOG_TAG, "Translucent status bar is enabled");
                this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }

            TaskDescription tDesc = new TaskDescription(null, null,
                    ContextCompat.getColor(this, Colorful.getThemeDelegate().getPrimaryColor().getColorRes()));
            setTaskDescription(tDesc);
        }
    }

    protected void onResume() {
        super.onResume();
        if(!Colorful.getThemeString().equals(mThemeString)) {
            LogUtils.d(LOG_TAG, "Theme change detected, restarting activity");
            recreateActivity();
        }
    }

    private void recreateActivity() {
        //Delaying activity recreate by 1 millisecond. If the recreate is not delayed and is done
        // immediately in onResume() you will get RuntimeException: Performing pause of activity that is not resumed
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                recreate();
            }
        }, 1);
    }

    public String getThemeString() {
        return mThemeString;
    }

}
`
meierjan commented 7 years ago

Can you fix the formatting issues and tell what you changed, please?

Elvis10ten commented 7 years ago

Sorry, i changed only the code in onResume from:

@Override
    protected void onResume() {
        super.onResume();
        if (!Colorful.getThemeString().equals(themeString)) {
            Log.d(Util.LOG_TAG, "Theme change detected, restarting activity");
            recreate();
        }
    }

to

protected void onResume() {
        super.onResume();
        if(!Colorful.getThemeString().equals(mThemeString)) {
            Log.d(LOG_TAG, "Theme change detected, restarting activity");
            recreateActivity();
        }
    }

    private void recreateActivity() {
        //Delaying activity recreate by 1 millisecond. If the recreate is not delayed and is done
        // immediately in onResume() you will get RuntimeException: Performing pause of activity that is not resumed
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                recreate();
            }
        }, 1);
    }

Still new to git so don't know how to submit my fix so am posting this here. The idea is to delay the call to recreate() in onResume() by using handler postDelayed method