ThemedPreferenceActivity.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Daniel Bailey
  5. * Copyright (C) 2019 Daniel Bailey
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.owncloud.android.ui.activity;
  21. import android.content.SharedPreferences;
  22. import android.os.Bundle;
  23. import android.preference.PreferenceActivity;
  24. import javax.inject.Inject;
  25. import androidx.annotation.Nullable;
  26. public class ThemedPreferenceActivity
  27. extends PreferenceActivity
  28. implements SharedPreferences.OnSharedPreferenceChangeListener {
  29. /**
  30. * Tracks whether the activity should be recreate()'d after a theme change
  31. */
  32. private boolean themeChangePending;
  33. private boolean paused;
  34. @Inject SharedPreferences sharedPreferences;
  35. @Override
  36. protected void onPostCreate(@Nullable Bundle savedInstanceState) {
  37. super.onPostCreate(savedInstanceState);
  38. sharedPreferences.registerOnSharedPreferenceChangeListener(this);
  39. }
  40. @Override
  41. protected void onDestroy() {
  42. super.onDestroy();
  43. sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
  44. }
  45. @Override
  46. protected void onPause() {
  47. super.onPause();
  48. paused = true;
  49. }
  50. @Override
  51. protected void onResume() {
  52. super.onResume();
  53. paused = false;
  54. if(themeChangePending) {
  55. recreate();
  56. }
  57. }
  58. @Override
  59. public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
  60. if(paused) {
  61. themeChangePending = true;
  62. return;
  63. }
  64. recreate();
  65. }
  66. }