how to change/reset custom theme from Java code in android - 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.

Related

Using overridePendingTransition() to animate the first Activity

I'm trying to use overridePendingTransition() to animate the very first Activity - the one that gets created when the user launches the app. Is this possible?
Where should I call overridePendingTransition()?
Tried right after super.onCreate() and right after super.onStart() , but it didn't work.
I would prefer to manage this at runtime, rather than setting the #anim in a custom style. Couldn't find anything. Is it possible?
from experience all I will say is it will not WORK to make it work you need to use a workaround, which is create a transparent launcher activity, carrying the app name and everything without a View (that is do not set content view) and make it transparent as I said with this code this.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); and immediately call your "in real life splash screen or launcher activity", and put this.overridePendingTransition(R.anim.grow_from_middle,R.anim.shrink_to_middle); in its onresume() (anywhere you prefer, I prefer this) and voila you have it..

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

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

Running an activity inside an activity?

I am developing a library for Android. This lib consist of a custom view. I'd like to be able to detect from my lib when onConfigurationChanged() is called for the activity.
My first thought was to use startActivity() with my own activity that only implement onConfigurationChanged(), but unfortunatly this start a new activity on top of the application. Is it possible to run an activity in "background"?
Maybe I am having the wrong approach? Do you have any idea of how I can achieve this?
So the way I was trying to solve it wasn't googd.
I added a method onConfigurationChanged() to my lib. That way, people using it will have to call this method from their activity.
Problem solved!
I had a similar problem - a custom Gallery that created its own dialog and needed to track configuration changes (for it leaked its views when orientation changed).
To solve this, dialog.dismiss() must had to be called when such a change was detected. I already needed main activity to call some methods of the custom view, onContextItemSelected and onActivityResult for instance.
I would like something more transparent to users. In this particular case, I achieved this by calling the dismiss method from the onSaveInstanceState from View (not from Activity), and keep the caller activity intact without having to call any more methods from the custom view.
You need to define in your manifest that the activity handles the onConfiguratoinChange and then define it in your code. You can check out the Google docs here for an example. http://developer.android.com/guide/topics/resources/runtime-changes.html

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.

When is it NOT okay to use onRetainNonConfigurationInstance()

Documentation of onRetainNonConfigurationInstance() says "This function is called purely as an optimization, and you must not rely on it being called."
Under what circumstances will onRetainNonConfigurationInstance() not be called?
Some background on what I am trying to achieve: I want to my activity to be notified of orientation change reliably (but also want activity to be killed & restarted as usual).
if you want the activity to restart (calling onCreate->onResume) again on orientation change you don't place configChanges="orientation" in the manifest. you can check the orientation of the screen in the onCreate method of your activity which is probably what you need. if you dont want the activity to restart itself but rather just switch orientation then you add the configChanges flag in the manifest for the activity and then only onConfigurationChanged() will be called where you also can get the screen orientation. the second way is good when you have expensive operations running in the onCreate methods (starting threads quering databases etc) and you want to reuse that info/data for portrait and for landscape. even if you want you can change the layout in onConfigurationChanged() the same way its done in on create but you again need to find the references to all the views because their ids are not the same in that case.
In any case if you want to have a reference to something that existed before the orentation change the configChanges way is better for handling the changes but it requires a bit more work if you are changing layouts or something.

Categories

Resources