Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update activity after uses changes appearance from preferences?

When a user opens preferences on my app he may make changes for example changing the app theme.

The documentation for ContextThemeWrapper.setTheme(int) says:

Set the base theme for this context. Note that this should be called before any views are instantiated in the Context (for example before calling setContentView(View) or inflate(int, ViewGroup)).

So my first thought was restarting the app onResume() when the user changed the preferences. However I noticed that sometimes the process of restarting the activity is seamless while other times the activity is closed, the home screen is seen and only after some seconds the app opens again.

I'm wondering if there is a way to change handle the preferences changes. Like for instance changing the theme after onResume without restarting the activity or restarting the activity on the background while the user is on preferences.

What's the right way to handle this?

like image 664
lisovaccaro Avatar asked Jan 03 '15 23:01

lisovaccaro


People also ask

How do I get my old Android activity back?

In the second activity, the back button at the top left can be used to go back to the previous activity.

How does an activity know about change in state?

Activity or dialog appears in foregroundIf a new activity or dialog appears in the foreground, taking focus and completely covering the activity in progress, the covered activity loses focus and enters the Stopped state. The system then, in rapid succession, calls onPause() and onStop() .

How do I change activity on Android?

Change Activity Type on AndroidGo to your Start tab in the app and locate the grid with 4 boxes at the bottom of the screen. Tap the Activity box and select the activity type you want to track (running, walking, cycling, hiking, etc.).


1 Answers

Assuming that your Preference screen is an Activity, when the user navigated to it, MainActivity was placed on a paused state (and then probably on a stopped state). When the user navigates back to MainActivity onResume() will be called; here you can change the sate of MainActivity accordingly to reflect the preferences that were changed.

like image 147
Emmanuel Avatar answered Oct 05 '22 16:10

Emmanuel