Skip to content

Commit

Permalink
fix maintain background color when recreated activity or fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
sakuna63 committed Aug 8, 2014
1 parent 3e635a9 commit 51827d4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
23 changes: 21 additions & 2 deletions sample/src/main/java/com/sample/SampleActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sample;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
Expand All @@ -14,6 +15,7 @@

public class SampleActivity extends ActionBarActivity implements ColorPickerCallback{
private static final String TAG = SampleActivity.class.getSimpleName();
private static final String SAVED_STATE_BACKGROUND_COLOR = "com.sample.SAVED_STATE_BACKGROUND_COLOR";

Caladbolg mCaladbolg;
RelativeLayout mLayout;
Expand All @@ -29,14 +31,31 @@ public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
if (savedInstanceState != null) {
mLayout.setBackgroundColor(savedInstanceState.getInt(SAVED_STATE_BACKGROUND_COLOR));
}
setContentView(mLayout);

if (getSupportFragmentManager().findFragmentByTag("caladbolg") == null) {
mCaladbolg = Caladbolg.getInstance(Color.BLACK);
mCaladbolg = (Caladbolg) getSupportFragmentManager().findFragmentByTag("caladbolg");
if (mCaladbolg == null) {
int color = Color.BLACK;
if (mLayout.getBackground() instanceof ColorDrawable) {
color = ((ColorDrawable) mLayout.getBackground()).getColor();
}
mCaladbolg = Caladbolg.getInstance(color);
mCaladbolg.show(getSupportFragmentManager(), "caladbolg");
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mLayout.getBackground() instanceof ColorDrawable) {
int color = ((ColorDrawable) mLayout.getBackground()).getColor();
outState.putInt(SAVED_STATE_BACKGROUND_COLOR, color);
}
}

@Override
public void onPickColor(int rgb, int alpha) {
Log.v(TAG, "RGB:" + rgb + " Alpha:" + alpha);
Expand Down
30 changes: 25 additions & 5 deletions sample/src/main/java/com/sample/SampleFragment.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sample;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
Expand All @@ -14,6 +15,8 @@
import com.caladbolg.Caladbolg.ColorPickerCallback;

public class SampleFragment extends Fragment implements ColorPickerCallback {
private static final String TAG = SampleFragment.class.getSimpleName();
private static final String SAVED_STATE_BACKGROUND_COLOR = "com.sample.SAVED_STATE_BACKGROUND_COLOR";

Caladbolg mCaladbolg;
LinearLayout mLayout;
Expand All @@ -23,10 +26,16 @@ public static SampleFragment newInstance() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getFragmentManager().findFragmentByTag("caladbolg") == null) {
mCaladbolg = Caladbolg.getInstance(this, Color.BLACK);
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

mCaladbolg = (Caladbolg) getFragmentManager().findFragmentByTag("caladbolg");
if (mCaladbolg == null) {
int color = Color.BLACK;
if (mLayout.getBackground() instanceof ColorDrawable) {
color = ((ColorDrawable) mLayout.getBackground()).getColor();
}
mCaladbolg = Caladbolg.getInstance(this, color);
mCaladbolg.show(getFragmentManager(), "caladbolg");
}
}
Expand All @@ -42,13 +51,24 @@ public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
if (savedInstanceState != null) {
mLayout.setBackgroundColor(savedInstanceState.getInt(SAVED_STATE_BACKGROUND_COLOR));
}

return mLayout;
}

@Override
public void onSaveInstanceState(Bundle outState) {
if (mLayout.getBackground() instanceof ColorDrawable) {
int color = ((ColorDrawable) mLayout.getBackground()).getColor();
outState.putInt(SAVED_STATE_BACKGROUND_COLOR, color);
}
}

@Override
public void onPickColor(int rgb, int alpha) {
Log.v(SampleFragment.class.getSimpleName(), "RGB:" + rgb + " Alpha:" + alpha);
Log.v(TAG, "RGB:" + rgb + " Alpha:" + alpha);
int argb = Color.argb(alpha, Color.red(rgb), Color.green(rgb), Color.blue(rgb));
mLayout.setBackgroundColor(argb);
}
Expand Down

0 comments on commit 51827d4

Please sign in to comment.