Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Commit

Permalink
CActivity automatically restarts on theme change
Browse files Browse the repository at this point in the history
  • Loading branch information
Garret Yoder committed Oct 5, 2016
1 parent 39be47c commit 026ff31
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
13 changes: 13 additions & 0 deletions library/src/main/java/org/polaric/colorful/CActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,30 @@
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.WindowManager;

public abstract class CActivity extends AppCompatActivity {
private String themeString;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
themeString=Colorful.getThemeString();
setTheme(Colorful.getThemeDelegate().getStyle());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (Colorful.getThemeDelegate().isTranslucent()) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
}

@Override
protected void onResume() {
super.onResume();
if (!Colorful.getThemeString().equals(themeString)) {
Log.d(Util.LOG_TAG, "Theme change detected, restarting activity");
recreate();
}
}
}
4 changes: 4 additions & 0 deletions library/src/main/java/org/polaric/colorful/Colorful.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public static ThemeDelegate getThemeDelegate() {
return delegate;
}

public static String getThemeString() {
return themeString;
}

public enum ThemeColor {
RED(R.color.md_red_500, R.color.md_red_700),
PINK(R.color.md_pink_500, R.color.md_pink_700),
Expand Down
11 changes: 0 additions & 11 deletions sample/src/main/java/org/polaric/colorful/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import android.support.v7.widget.Toolbar;

import org.polaric.colorful.CActivity;
import org.polaric.colorful.ColorPickerDialog;
import org.polaric.colorful.Colorful;

public class MainActivity extends CActivity {

Expand All @@ -14,14 +12,5 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar(((Toolbar) findViewById(R.id.toolbar)));

ColorPickerDialog dialog = new ColorPickerDialog(this);
dialog.setOnColorSelectedListener(new ColorPickerDialog.OnColorSelectedListener() {
@Override
public void onColorSelected(Colorful.ThemeColor color) {
//TODO: Do something with the color
}
});
dialog.show();
}
}

0 comments on commit 026ff31

Please sign in to comment.