diff --git a/sample/src/main/java/com/sample/SampleActivity.java b/sample/src/main/java/com/sample/SampleActivity.java index 49088d6..a439982 100644 --- a/sample/src/main/java/com/sample/SampleActivity.java +++ b/sample/src/main/java/com/sample/SampleActivity.java @@ -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; @@ -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; @@ -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); diff --git a/sample/src/main/java/com/sample/SampleFragment.java b/sample/src/main/java/com/sample/SampleFragment.java index 48425a9..48b7c57 100644 --- a/sample/src/main/java/com/sample/SampleFragment.java +++ b/sample/src/main/java/com/sample/SampleFragment.java @@ -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; @@ -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; @@ -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"); } } @@ -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); }