Исполнение кода один раз при запуске приложения (Android)

Делаю в своём приложении изменение темы (день/ночь). При этом я использую preferences для хранения данных о текущей теме. Но я не могу, и не знаю как сделать, чтобы при запуске приложения в зависимости от данных хранящих preference о прошлой теме, запускалась какая-либо тема (день/ночь).

MainActivity:

package com.example.arseny.myapplication;
 
import ...
 
public class MainActivity extends AppCompatActivity {
 
    TextView textView;
 
    SharedPreferences sPref;
 
    final String SAVED_TEXT = "saved_text";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        textView = (TextView)findViewById(R.id.textView);
 
        sPref = getPreferences(MODE_PRIVATE);
        String saveText = sPref.getString(SAVED_TEXT, "");
 
        textView.setText(saveText);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch(id){
            case R.id.action_settings :
                one();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
 
    public void one(){
        int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
        sPref = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor ed = sPref.edit();
 
        switch (currentNightMode) {
            case UI_MODE_NIGHT_NO:
                ed.putString(SAVED_TEXT, "2");
                ed.commit();
 
                AppCompatDelegate.setDefaultNightMode (AppCompatDelegate.MODE_NIGHT_YES);
                recreate();
                return;
 
            case UI_MODE_NIGHT_YES:
                ed.putString(SAVED_TEXT, "1");
                ed.commit();
 
                AppCompatDelegate.setDefaultNightMode (AppCompatDelegate.MODE_NIGHT_NO);
                recreate();
                return;
        }
    }
 
    public void onMyButtonClick(View view) {
        int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
        switch (currentNightMode) {
            case UI_MODE_NIGHT_NO:
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, com.example.arseny.myapplication.Main2Activity.class);
                intent.putExtra("tema", "1");
                startActivity(intent);
                return;
 
            case UI_MODE_NIGHT_YES:
                Intent intentt = new Intent();
                intentt.setClass(MainActivity.this, com.example.arseny.myapplication.Main2Activity.class);
                intentt.putExtra("tema", "2");
                startActivity(intentt);
                return;
        }
    }
}

Рад буду помощи!


Ответы (1 шт):

Автор решения: Maks

Здесь нету ничего сложного. просто нужно сохранять состояние при onDestroy и возобновлять в onCreate. Вот например ролик как это сделать:

https://www.youtube.com/watch?v=xqY7Yu5C8pg

→ Ссылка