How can I retain same state in landscape mode in android - android

How can I retain same state in landscape.
when I rotted the screen in landscape mode then activity is restarted and
my layout view is showing wrong. so please help me.

you may save your state in a Bundle in onSaveInstanceState() method and restore it back in onCreate() or in onRestoreInstanceState().
or if you want to disable orientation changes, you may add android:screenOrientation="landscape" to your Activity in the manifest file.

If your speaking that your layout doesnt look/fit properly in landscape mode then you will need a new xml with the same name & place it inside a new folder called "layout-land". That should help

For retain same state in landscape mode you must add android:configChanges="orientation|keyboard" in your Current Activity to Handle Configuration Change in your on way as:
<activity
android:configChanges="orientation|keyboard"
...
/>
or for Changing Layout or any View on configChanges you must override onConfigurationChanged of Activity as:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
//Handle config changes here, paying attention to
//the newConfig.orientation value
super.onConfigurationChanged(newConfig);
}
and if you want to save state on any view on Configuration Chnages then you must override onSaveInstanceState() for saving state of view like TextView, EditText,View Layout's .. and onRestoreInstanceState() for Retrieving state back after Device mode rotation.
for more info about how we handle runtime changes you can also see this android topic :
Handling Runtime Changes

Related

Make Multiple Activites Landscape If App started as Landscape

What is Problem?
if app started as portrait then make activity as portrait , otherwise landscape , but don't detect sensor based or used based rotation otherwise.
My Solution:
Make variable isPortrait in application class and detect orientation in onCreate of Application class then use this variable in all activites to detect what orientation to use.
Problem in My Solution:
No Idea how can I change different configuration layouts and map them accordingly in this scenario , what if user change it orientation to landscape after app started in portrait ? is there any ActivityInfo flag to achieve this behaviour.
use this in your activity tag in manifest
android:configChanges="orientation"
android:screenOrientation="portrait"
In Mainfest within the atvitity tag add below code
android:configChanges="orientation|screenSize"
and in ur activity override onConfiguration method like below
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
log.e("onConfigurationChanged", "screen orientation change");
}

Android:stop refresh Activity when page move onto portrait to landscape mood

I want to stop activity refresh when I move on to portrait mood to landscape mood and I also want load file layout-land when it move to portrait to landscape mood without any refreshment the activity.
I use <activity android:name=".Login"
android:configChanges="orientation|screenSize">
but,In this method when I move onto portrait to landscape mood ,It does not load the file from layout-land folder. what should I do for this? please someone help me.
Call method setContentView(R.layout.main) in onConfigurationChanged()
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
///in case you have some EditTexts , save their input before setting the new layout.
emailForConfigChanges = emailTextBox.getText().toString().trim();
passwordForConfigChanges = passwordTextBox.getText().toString().trim();
setContentView(R.layout.main);
//Now set the values back to the EditTexts .
}
You just have to add this method to your activity as it handles all the orientation changes if you have declared "orientation" in your manifest.
EDIT : And if EditTexts lose their values on rotation, get their values before calling setContentView() in onConfigurationChanged(). See the edit above.
I handled this problem only by addding android:configChanges="keyboardHidden|orientation"
hope this will help you.

How save WebView when screen orientation was changed

In my application I use only WebView for loading index.html where running all app logic(html's and javascript).
Where screen orientation changed, all data from JavaScript variables disappear.
How I can save all data when orientation changed?
You have to catch orientation changes and handle them by yourself, because by default the Activity gets recreated (and your WebView will be reloaded).
Add the android:configChanges attribute to your Activity in the AndroidManifest.xml
<activity ... android:configChanges="orientation|keyboardHidden|keyboard">
And override the onConfigurationChanged method in your Activity class.
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
Also have a look here: Activity restart on rotation Android

Android : Save application state on screen orientation change

I have seen the following links before posting this question
http://www.devx.com/wireless/Article/40792/1954
Saving Android Activity state using Save Instance State
http://www.gitshah.com/2011/03/how-to-handle-screen-orientation_28.html
How to save state during orientation change in Android if the state is made of my classes?
I am not getting how should i override the following function :
#Override
public Object onRetainNonConfigurationInstance() {
return someExpensiveObject;
}
In my application i have layout with one editext visible and other editext get visible when the data of first editext validates to true.I have set the visbility of all other editextes and textviews to false and make them visible after validating.
So in my activity if the screen orientation is changed then all the items having android:visibility="false" get invisible.
I have also came to know that when our activities screen orientation changes it calls onStop() followed by onDestroy() and then again starts a fresh activity by calling onCreate()
This is the cause .. But i am not getting how to resolve it ..
Here You can see the screenshots of my application :
in this image all fields are loaded
and in another image when the screen orientation is changed to landscape they are all gone
Any link to tutorial or piece of code will be highly appreciable.
And also my application crashes when a progress dialog is shown up and i try to change screen orientation.How to handle this ??
Thanks
Well if you have the same layout for both screens then there is no need to do so just add below line in your manifest in Activity node
android:configChanges="keyboardHidden|orientation"
for Android 3.2 (API level 13) and newer:
android:configChanges="keyboardHidden|orientation|screenSize"
because the "screen size" also changes when the device switches between portrait and landscape orientation.
From documentation here: http://developer.android.com/guide/topics/manifest/activity-element.html
There is another possibility using which you can keep the state as it is even on Orientation change using the onConfigurationChanged(Configuration newConfig).
Called by the system when the device configuration changes while your activity is running. Note that this will only be called if you have selected configurations you would like to handle with the configChanges attribute in your manifest. If any configuration change occurs that is not selected to be reported by that attribute, then instead of reporting it the system will stop and restart the activity (to have it launched with the new configuration).
At the time that this function has been called, your Resources object will have been updated to return resource values matching the new configuration.
There are 2 ways of doing this, the first one is in the AndroidManifest.xml file. You can add this to your activity's tag. This documentation will give you an in depth explanation, but put simply it uses these values and tells the activity not to restart when one of these values changes.
android:configChanges="keyboardHidden|orientation|screenSize|screenLayout"
And the second one is: overriding onSaveInstanceState and onRestoreInstanceState. This method requires some more effort, but arguably is better. onSaveInstanceState saves the values set (manually by the developer) from the activity before it's killed, and onRestoreInstanceState restores that information after onStart() Refer to the official documentation for a more in depth look. You don't have to implement onRestoreInstanceState, but that would involve sticking that code in onCreate().
In my sample code below, I am saving 2 int values, the current position of the spinner as well as a radio button.
#Override
public void onSaveInstanceState(#NonNull Bundle savedInstanceState) {
spinPosition = options.getSelectedItemPosition();
savedInstanceState.putInt(Constants.KEY, spinPosition);
savedInstanceState.putInt(Constants.KEY_RADIO, radioPosition);
super.onSaveInstanceState(savedInstanceState);
}
// And we restore those values with `getInt`, then we can pass those stored values into the spinner and radio button group, for example, to select the same values that we saved earlier.
#Override
public void onRestoreInstanceState(#NotNull Bundle savedInstanceState) {
spinPosition = savedInstanceState.getInt(Constants.KEY);
radioPosition = savedInstanceState.getInt(Constants.KEY_RADIO);
options.setSelection(spinPosition, true);
type.check(radioPosition);
super.onRestoreInstanceState(savedInstanceState);
}

How to save tabhost tabs between destroy-create cycle

Application has a tabhost managed through TabActivity.
It has option to add tabs at runtime.
Say 5 tabs are added in runtime and different activities are shown.
When I rotate the screen the activity undergoes the cycle o destroy and create.
I want to maintain the tabs added by user in runtime to be available during this cycle.
The easiest way to do this is to change your Manifest to say that you will handle the orientation change yourself.
<activity
android:name=".MyActivity"
android:configChanges="orientation" />
What this does is tell the system to not recreate the activity on orientation change. You can then override OnOrientationChanged to modify any configuration changes.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//put configuration changes here
}
You can also leave it out if you don't need any explicit changes.
For further reading: Android Runtime Changes

Categories

Resources