onConfigurationChanged() is not called when Dark theme changes - android

Firstly, I have searched StackOverFlow for many hours before I post this question.
I tried to get onConfigurationChanged() called when user changes the theme in Setting but I've got no luck, event for other configs such as: Orientation, Screen size...
Please show me where I am doing wrong.
**Manifest file : use OnConfigChanges for handling config change event **
<activity
android:name=".Notification.OnConfigChanges"
android:configChanges="keyboardHidden|orientation|screenSize|uiMode"
android:label="#string/app_name">
</activity>
OnConfigChanges class:
public class OnConfigChanges extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_on_config_changes);
Log.d("onConfig","Yes");
Toast.makeText(this,"onConfig",Toast.LENGTH_SHORT).show();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d("onConfig","Yes");
Toast.makeText(this,"onConfig",Toast.LENGTH_SHORT).show();
}

Related

How to work with AsyncTask when screen rotates

I have an activity in my android app that calls an asynctask on onCreateView()
Here is my code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weeklyprofit);
fromDateTxt=(TextView)findViewById(R.id.fromDate);
toDateTxt = (TextView)findViewById(R.id.toDate);
GetXYPoints getXY = new GetXYPoints();
getXY.execute(fromDateTxt.getText().toString(),toDateTxt.getText().toString());
}
now my application needs to rotate, so i need to store data when rotate :
I have implemented this as below :
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("from", fromDateTxt.getText().toString());
savedInstanceState.putString("to", toDateTxt.getText().toString());
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
SystemClock.sleep(500);
fromDateTxt.setText(savedInstanceState.getString("from"));
toDateTxt.setText(savedInstanceState.getString("to"));
GetXYPoints getXY = new GetXYPoints();
getXY.execute(fromDateTxt.getText().toString(),toDateTxt.getText().toString());
}
So i recall the asynctask again with restored data, my problem is that the activity run again when rotate and the it calls onRestoreInstanceState so how can i prevent the activity from calling the first asyncktask?
In other way what is the best solution to store data returned by an asynck task when screen is rotate?
i would suggest you to to make sure you actually need your activity to be reset on a screen rotation (the default behavior). Every time I've had issues with rotation I've added this attribute to my tag in the AndroidManifest.xml, and been just fine.
android:configChanges="keyboardHidden|orientation"
source How to handle an AsyncTask during Screen Rotation?
In your AndroidManifest.xml add following for prevent reloading activity when orientation change
android:configChanges="orientation|screenSize"
So, you'll have something like this:
<activity android:name="Activity"
android:configChanges="orientation|screenSize">
</activity>
Hope it works!

lock android activity in portrait mode without doing it for each activity

So I know that I can do android:screenOrientation="portrait" for each activity in my manifest and that will lock the activity to portrait mode. But I am wondering if there is a place in the manifest where I can enter this once and have it apply to the entire application.
Create a custom activity
public class CustomActivity extends Activity{
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
}
Then for all activities you create , for example ActicityA,ActivityB extend it from CustomActivity .
public class ActicityA extends CustomActivity {}
public class ActivityB extends CustomActivity {}
Create a custom Activity in which you define the orientation and fix it to your desired position.
Inherit all the Activities in your application from your CustomActivity which has been defined with a fixed orientation.
EDIT : This answer provides yet another way of doing the same thing, but I like to believe that mine is more elegant because you wouldn't need to call the function for each and every Activity.
In you manifest file you can give like this
<activity
android:name="com.androidcalapp.MainActivity"
android:label="#string/app_name"
android:screenOrientation="landscape">
As far as I know we have to repeat it for every activity in manifest.
Or else you can handle it at runtime, by overriding onConfigurationChanged()method in Activity.
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

Android : How to forbird re-creation of Activity when orientation changed?

I have an Android Activity with Frgment. When my application is started, a listview is loaded with 10 elements which are sent from a server. (simple request).
When i change the orientation of my device, the activity is recreated, i see also the spinner with my request to load my listview.
How can i do to forbid re-creation of Activity ?
I already set the flag :
android:configChanges="orientation|keyboardHidden|screenSize"
And onConfigurationChanged :
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
You then want to override
#Override
public void onConfigurationChanged(Configuration newConfig)
{
}
in your activity, with what you want to do on orientation/keyboardHidden/screenSize.
Setting the flag in the manifest tells the app that you want to handle those configuration changes yourself.
You need to override onSaveInstanceState(Bundle savedInstanceState) and keep state of all the values that you need when orientation changes.
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putString("jsonArrayString", jsonArrayStrin);
// etc.
}
This value will be passed to the onCreate and/or onRestoreInstanceState. You can retrieve this using following :
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// This bundle has also been passed to onCreate.
String myArrayString = savedInstanceState.getString("jsonArrayString");
}
Hope this helps
If you don't need the orientation to change, you can also prevent it in the manifest. Just specify in the activity element the attribute: android:screenOrientation

The advice rotation charges again my webviews class

When I rotate my devices, this charge again his class. For example, if I have a google search when I change the orientation charges again to google.
I've try it with:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.principal);
}
and onRestoreInstanceState and onSaveInstanceState. But my problem persist. I would appreciate if somebody can help me with a example or explication of how can to do it.
Thank's!
I've solved the problem, I needed to do the if (savedInstanceState == null) :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.principal);
webview.setWebViewClient(new WebViewClient());
if (savedInstanceState == null)
{
webview.loadUrl("http://www.apple.es");
}
}
#Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
// Save the state of the WebView
webview.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
// Restore the state of the WebView
webview.restoreState(savedInstanceState);
}
I hope than my problem and my solution can help to somebody!
Christian you have to see this doc on android developer web site and learn how romain guy stores his last loaded image and call them when the configuration screen changes!! the solution is to use
onRetainNonConfigurationInstance()
getLastNonConfigurationInstance()
I also refer to this tutorial always to get rid of the screen change behavour of android
hope this will help you
Add configChanges in your AndroidManifest file.
For example:
<activity android:name="YourActivityName"
android:configChanges="orientation">
</activity>
and save your current url in local variable and load it in onConfigChanges
#Override
public void onConfigurationChanged(Configuration newConfig) {
yourWebView.loadUrl(yourUrl);
}
use following piece of code
public void onConfigurationChanged(Configuration newConfig) {
yourWebView.loadUrl(yourUrl);
}
Refer this link
Activity restart on rotation Android

Android Phonegap screen rotation lock kills my app

i'm having problems with my application, i added an manifest that must prevent screen rotation but when i rotate the screen it kills the app.
Here is my code:
public class avantdroidActivity extends DroidGap {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
super.clearCache();
super.loadUrl("file:///android_asset/www/redir.html");
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
My AndroidManifest.Xml:
<activity android:name="org.apache.cordova.DroidGap" android:label="#string/app_name" android:configChanges="keyboard|orientation|keyboardHidden"> <intent-filter> </intent-filter> </activity>
What im doing wrong? Thanks!
Why are you calling?
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
You can just set your activity orientation in your AndroidManifest.
Just add android:screenOrientation="portrait" to your activity tag.

Categories

Resources