DHerls
DHerls

Reputation: 877

Recreating Activity causes infinite loop

I am trying to change the theme of my app, so I call the following methods when the user presses a button:

activity.setTheme(R.style.BlueTheme);
activity.recreate();

However, whenever this happens, the app goes into an infinite loop with the console output being:

02-01 11:36:15.077 9245-9276/org.example.androidscouting E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab7d5680
02-01 11:36:15.131 9245-9276/org.example.androidscouting W/EGL_emulation: eglSurfaceAttrib not implemented
02-01 11:36:15.131 9245-9276/org.example.androidscouting W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa3170780, error=EGL_SUCCESS

And the above just loops forever until I manually close the app.

Could anyone tell me what the issue is and how to fix it, or at least point me in the right direction?

EDIT: Additional code

On Create:

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Utils.onActivityCreateSetTheme(this);
    setContentView(R.layout.activity_scouting);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    LinearLayout l = (LinearLayout) findViewById(R.id.mainLinear);
    l.requestFocus();

    //createTheApp();

    Switch colorSwitch = (Switch) findViewById(R.id.team_color);
    colorSwitch.setOnCheckedChangeListener(this);


}

onCheckedChange():

    @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked){
        Utils.changeToTheme(this,Utils.THEME_RED);

    } else {
        Utils.changeToTheme(this, Utils.THEME_BLUE);
    }
}

Utils.java - Adapted from another quetion

import android.app.Activity;
import android.content.Intent;

public class Utils
{
    private static int sTheme = 2;
    public final static int THEME_RED = 1;
    public final static int THEME_BLUE = 2;

    public static void changeToTheme(Activity activity, int theme)
    {
        sTheme = theme;
        activity.recreate();
    }
    /** Set the theme of the activity, according to the configuration. */
    public static void onActivityCreateSetTheme(Activity activity)
    {
        switch (sTheme)
        {
            default:
            case THEME_BLUE:
                activity.setTheme(R.style.BlueTheme);
                break;
            case THEME_RED:
                activity.setTheme(R.style.RedTheme);
                break;
        }
    }
}

Styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />


<style name="BlueTheme" parent="AppTheme.NoActionBar" >
    <item name="colorPrimary">@color/colorBluePrimary</item>
    <item name="colorPrimaryDark">@color/colorBluePrimaryDark</item>
    <item name="colorAccent">@color/colorBlueAccent</item>
</style>

<style name="RedTheme" parent="AppTheme.NoActionBar" >
    <item name="colorPrimary">@color/colorRedPrimary</item>
    <item name="colorPrimaryDark">@color/colorRedPrimaryDark</item>
    <item name="colorAccent">@color/colorRedAccent</item>
</style>

Upvotes: 3

Views: 1117

Answers (1)

thedarkpassenger
thedarkpassenger

Reputation: 7358

I think the problem is in this method

     @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked){
        Utils.changeToTheme(this,Utils.THEME_RED);
        //Assign isChecked = false;

    } else {
        Utils.changeToTheme(this, Utils.THEME_BLUE);
        // Assign isChecked = true;
    }
}

Upvotes: 1

Related Questions