Apply any theme change on return to MainActivity
This commit is contained in:
parent
980dc10d67
commit
690a7f3aa0
@ -34,6 +34,8 @@ public class MainActivity extends BaseActivity {
|
||||
public static final int PAGE_STOPWATCH = 2;
|
||||
public static final String EXTRA_SHOW_PAGE = "com.philliphsu.clock2.extra.SHOW_PAGE";
|
||||
|
||||
public static final int REQUEST_THEME_CHANGE = 5;
|
||||
|
||||
/**
|
||||
* The {@link android.support.v4.view.PagerAdapter} that will provide
|
||||
* fragments for each of the sections. We use a
|
||||
@ -253,6 +255,8 @@ public class MainActivity extends BaseActivity {
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (resultCode != RESULT_OK)
|
||||
return;
|
||||
// If we get here, either this Activity OR one of its hosted Fragments
|
||||
// started a requested Activity for a result. The latter case may seem
|
||||
// strange; the Fragment is the one starting the requested Activity, so why
|
||||
@ -298,6 +302,14 @@ public class MainActivity extends BaseActivity {
|
||||
// DUPLICATE TIMERS.
|
||||
// mSectionsPagerAdapter.getFragment(mViewPager.getCurrentItem())
|
||||
// .onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
switch (requestCode) {
|
||||
case REQUEST_THEME_CHANGE:
|
||||
if (data != null && data.getBooleanExtra(SettingsActivity.EXTRA_THEME_CHANGED, false)) {
|
||||
recreate();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -324,7 +336,7 @@ public class MainActivity extends BaseActivity {
|
||||
|
||||
//noinspection SimplifiableIfStatement
|
||||
if (id == R.id.action_settings) {
|
||||
startActivity(new Intent(this, SettingsActivity.class));
|
||||
startActivityForResult(new Intent(this, SettingsActivity.class), REQUEST_THEME_CHANGE);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -9,6 +9,7 @@ import com.philliphsu.clock2.R;
|
||||
* Created by Phillip Hsu on 6/6/2016.
|
||||
*/
|
||||
public class SettingsActivity extends BaseActivity {
|
||||
public static final String EXTRA_THEME_CHANGED = "com.philliphsu.clock2.settings.extra.THEME_CHANGED";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
package com.philliphsu.clock2.settings;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.media.AudioManager;
|
||||
import android.media.Ringtone;
|
||||
@ -9,17 +11,25 @@ import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.preference.Preference;
|
||||
import android.preference.PreferenceFragment;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.preference.RingtonePreference;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import com.philliphsu.clock2.R;
|
||||
|
||||
public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
private String mInitialTheme;
|
||||
private SharedPreferences mPrefs;
|
||||
|
||||
public SettingsFragment() {}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
// We don't have a menu, but this is needed so we can receive callbacks to the options menu.
|
||||
// The only callback we are interested in is onOptionsItemSelected().
|
||||
setHasOptionsMenu(true);
|
||||
addPreferencesFromResource(R.xml.preferences);
|
||||
// Set ringtone summary
|
||||
setSummary(getPreferenceScreen().getSharedPreferences(), getString(R.string.key_timer_ringtone));
|
||||
@ -35,6 +45,8 @@ public class SettingsFragment extends PreferenceFragment implements SharedPrefer
|
||||
return true;
|
||||
}
|
||||
});
|
||||
mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
||||
mInitialTheme = mPrefs.getString(keyOfThemePreference(), "");
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -51,9 +63,43 @@ public class SettingsFragment extends PreferenceFragment implements SharedPrefer
|
||||
.unregisterOnSharedPreferenceChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
/*
|
||||
* https://developer.android.com/guide/topics/ui/menus.html#RespondingOptionsMenu
|
||||
* "If your activity includes fragments, the system first calls onOptionsItemSelected()
|
||||
* for the activity then for each fragment (in the order each fragment was added)
|
||||
* until one returns true or all fragments have been called."
|
||||
*/
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
String selectedTheme = mPrefs.getString(keyOfThemePreference(), "");
|
||||
Intent result = new Intent();
|
||||
result.putExtra(SettingsActivity.EXTRA_THEME_CHANGED, !selectedTheme.equals(mInitialTheme));
|
||||
getActivity().setResult(Activity.RESULT_OK, result);
|
||||
return false; // Don't capture, proceed as usual
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
setSummary(sharedPreferences, key);
|
||||
// --------------------------------------------------------------------------------------
|
||||
// TOneverDO
|
||||
//
|
||||
// A new instance of the activity would be created and onCreate() called again.
|
||||
// Our reading of the initial theme for the previous instance, which would be destroyed,
|
||||
// would not be retained.
|
||||
// Reading the initial theme *for the new instance* would give us the theme
|
||||
// that the previous instance had just changed to, prior to its recreate().
|
||||
// As such, the result passed back *by the new instance* via the up button
|
||||
// would always indicate the theme didn't change.
|
||||
// if (key.equals(keyOfThemePreference())) {
|
||||
// getActivity().recreate();
|
||||
// }
|
||||
// --------------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
private void setSummary(SharedPreferences prefs, String key) {
|
||||
@ -66,4 +112,8 @@ public class SettingsFragment extends PreferenceFragment implements SharedPrefer
|
||||
pref.setSummary(ringtone.getTitle(getActivity()));
|
||||
}
|
||||
}
|
||||
|
||||
private String keyOfThemePreference() {
|
||||
return getString(R.string.key_theme);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user