When to apply design-changes from PreferenceActivity in MainActivity? #onResume? - android

i`m new to android-coding and am working on my first app. I have the following question:
If one wants the user to be able (for instance) to change the backgroundcolor of the app in PreferenceActivity, where should this change be applied in the MainActivity of the app?
In my case i have many views on a few pages which i want to be customizable in one or the other way. I do it like this:
If there are relevant changes in PreferenceActivity i set a flag and save the changes and the flag in SharedPreferences
Since onResume of MainActivity is called whenever the user returns, i read from the flag in SharedPreferences if changes of the views need to be made and if so, i apply the changes.
I want to use a flag because i dont want to apply the changes again and again whenever onResume is called because its many views that are affected and i dont want to slow down the app unnecessarily.
How would you all do this? i`ll be happy about any hint. Maybe i should even apply the changes in PReferenceActivity already? i dont know......
thx

Related

Where to put preference based layout operations in ActionBarActivity

In my app I am creating a custom preference screen. This screen in reached via mainActivity (launcher). The mainActivity also shows the current status of some of these settings by reading them and laying them on views. Now, when the user will reach one of the preference screens and edit them, by design I aim to bring him back to the mainActivity (it would be intuitive to press the back button). This time however I want to show the latest edited settings.
I am used to putting layout operations in onCreate. But, in this case onCreate will only be called once when the activity starts and read the status of preference settings and lay them on screen. However, when he will open a settings activity, edit them and press back button he will not see his latest settings laid on screen as onCreate need not be called.
So, on what activity callback should I place the operations to read preferences and lay them on views.
This is based on my understanding and I may have messed up big time. Guide me, Thanks...
To anyone looking for an answer. Its pretty basic stuff. Create function in your main activity to read preferences and update your views. Now, start your preference activity using startActivityforResult() and when result arrives from that activity, run the same function inside onActivityResult(). Done.

Widget Preferences "delay" problem

I have a Widget with his android.appwidget.action.APPWIDGET_CONFIGURE Activity with some preferences witch I can set before adding it to the "desktop".
My problem is that the preferences does not get applied because I noticed that as soon as the APPWIDGET_CONFIGURE is called, the widget is created and never refresh after the widget is actually applied to the screen and it actually never reads the updated preferences.
Any thoughts?
Thanks
Oh, I think I found what it seems to be an easy solution but I don't like it much so I like to have a "second opinion". I don't say what it is to not influence the answers.
PS. The configuration activity works fine, the problem is only when the widget is applied for the first time.
The (easy) solution I found is to call an update on the widget after closing the APPWIDGET_CONFIGURE Activity.
The "dirty" thing I don't like is that you actually "draw" the widget one extra time.

Override Orientation Change But NOT Restart The Activity AND Pass State Data

I want to be able to change the layout when a device is re-orientated to landscape or portrait. For speed and resource purposes (plus other issues applicable to my app) I do NOT want my app to be destroyed and restarted. I have several objects which I wish to retain between orientation changes as there is no benefit from destroying and re-creating them! I simply just want to change the position of some buttons and TextViews so that they suit the current orientation. Easy right?
Well no it isn't. To achieve the above I included in the app Manifest the configChange option for orientation change. Then I've implemented the onConfigurationChanged() where I determine and apply the appropriate layout. Simple yes?
But now take the textview I have in my layout. How on earth, using this particular method of responding to orientation changes, do I put the same text in the previous textview to the new textview? No instance data is passed to onConfigurationChanged() method. Also for some of the buttons, they could be disabled or enabled... I need to know this after orienatation change.
If I let Android destroy and restart my activity it's first going to create unnecessary work. All I want is to just move a few buttons and textviews.. NOT restart the whole app. That's just ludicrous!
Can anyone help me achieve what need?
An easy way to maintain configuration-independent data is to make use of onRetainNonConfigurationInstance() and its companion method getLastNonConfigurationInstance(). Just return an object that contains all the data that you want to reuse when your activity is recreated.
In Honeycomb, or if you are using the Android compatibility package, you can just call Fragment.setRetainInstance(true) instead. See the docs.

how to change/reset custom theme from Java code in android

I have a change theme option in setting screen of my application and providing some custom themes to choose from .
first of all i believe you can't set theme to entire app from your java code at once (please guide if there is any way to do so ) , thats why i am calling setTheme(my_theme) before super.onCreate() in every activity of app .
Now when user change activity, this will reflect only at the time of relaunching any activity (becouse setTheme() is in OnCreate() ).
So issue is how to let SetTheme() works in OnResume() or anywhere else in code , because i want to reflect these changes on previous screens in Activity Stack also .
note that setTheme() works before setContentVIew() only ......
Yeah, as the docs say, you need to set the theme before any views are instantiated, so it looks like you will need to restart your entire activity.
There's probably a better way, but one way to ensure your activities completely restart in onResume():
finish();
startActivity(getIntent());
This will recycle the existing intent. However, I would first look around to see if there is a simpler way to ensure activities restart- might be a simple manifest property. Let us know what you find.

Responding to preference updates in Android

I am calling a PreferenceActivity from another activity and then updating the application state (ie: changing the font size) on onActivityResult, based on the preference changes.
I was thinking it would be better to put the state update logic in the PreferenceActivity. That way I don't have the duplicate the logic in each activity that calls the PreferenceActivity.
What's the best or correct way to do this?
Have any Activity (or other component) that cares about preference changes register a preference change listener via registerOnSharedPreferenceChangeListener(). Then, when the preferences change by any means, they will find out about it and can react accordingly.
The PreferenceActivity should handle all the preference setting. Your other activities should read what those settings are when they run and adjust themselves accordingly.

Categories

Resources