I have created layout and layout-land for both portrait and landscape view in android. I want that not refreshing the activity how i switch from layout to layout-land . As i have put android:configChanges="keyboardHidden|orientation|screenSize" in the manifest that prevent to switch from layout to layout-land.
I want:
Activity not refresh when change orientation
Switch XML from layout to layout-land or vice versa when change orientation
You need to use onSaveInstanceState() to save data which you need to bind when application config. will change and you can rebind that saved data in onRestoreInstanceState() method. here given link will helps you.
http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/
Note : you need to remove android:configChanges="keyboardHidden|orientation|screenSize" from menifest file file you want to call onConfigurationChanged()
You can store data in Bundle of activity in onSaveInstanceState() method like
protected void onSaveInstanceState(Bundle icicle) {
super.onSaveInstanceState(icicle);
icicle.putLong("param", value);
}
And you can retrieve that data in
public void onCreate(Bundle icicle) {
if (icicle != null){
value = icicle.getLong("param");
}
}
In short: you can't achieve this(only with some hacks). Take a look at Android Activity Lifecycle: android destroys activty when oriantation changes. So you cant switch from portrait to land (and back) without reinstantiating activity.
Related
I have 3 Layouts in different folders :
1.layout/HomeLayout.axml (where layout is an folder for layouts and HomeLayout.axml is an global axml file).
2.layout-large-port/HomeLayout.amxl (where layout-large-port is an folder for large(dimension of display) portrait and HomeLayout.axml is axml file).
3.layout-large-land/HomeLayout.axml(where is layout-large-land is an folder for large(dimension of display) landscape and HomeLayout.axml is axml file).
On this activity,i got ListView with some data and when i rotate my phone from portrait to landscape , i need to save all what i maked on Portrait mode(eachrow on listview have different things like buttons/textview and etc) and show on Landscape.
So what is done at current moment. On HomeActivity i declared this :
ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation
This snippet works well,but with big problem : after rotation(from Portrait to Landscape) remains layout Portrait. How can i fix that?
Also i tried to override method OnConfigurationChanged :
public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
{
base.OnConfigurationChanged (newConfig);
if(newConfig.Orientation == Android.Content.Res.Orientation.Landscape)
{
SetContentView(Resource.Layout.ProductsLayout); //system understand that now is Landscape and put correct layout;
}
else
{
SetContentView(Resource.Layout.ProductsLayout); //same here
}
So this method doesnt help me,because when i'm rotating phone,layout sets correct but with no data(listview is empty) and after that,when i'm trying to go back from Landscape to Portrait,layout sets correct but with empty list.
Any suggestions?
PS Sorry for my eng!
Android will completely destroy and recreate your activity when a configuration changes; all views in that activity and the data they hold will also be discarded.
From the Android docs:
Caution: Your activity will be destroyed and recreated each time the
user rotates the screen. When the screen changes orientation, the
system destroys and recreates the foreground activity because the
screen configuration has changed and your activity might need to load
alternative resources (such as the layout).
The standard mechanism for persisting data between configuration changes is to save the view data using the OnSaveInstanceState callback of your Activity and then restore the data using the bundle provided through the OnCreate callback.
This is a rough example of how to do so:
public class MainActivity : Activity
{
public const string ARG_LIST_STATE = "list_state";
ListView _listView;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
if (bundle != null) {
// Existing data to restore!
if (bundle.ContainsKey (ARG_LIST_STATE)) {
var listState = bundle.GetStringArray (ARG_LIST_STATE);
// TODO: Restore state into list view.
}
}
}
protected override void OnSaveInstanceState (Bundle outState)
{
string[] listState= new string[5]; // TODO: Grab data from the list view and serialize it into the outState
outState.PutStringArray(ARG_LIST_STATE, listState);
base.OnSaveInstanceState (outState);
}
}
Another example is provided by Xamarin here.
I strongly recommend that you review the activity lifecycle docs so you can better understand how activities are managed by Android.
Solution was(For me) to make acceptable design for portrait,which is it does not differ landscape mode(just use mechanisms like sum_weight and etc).
if the question sounds weird at first, here comes the explanation:
I have got an activity that hosts my three fragments. Since I would like one of my fragments to save its instance state when the device is rotated, I defined this in my manifest for my activity that hosts the fragments:
android:configChanges="orientation|screenSize"
This works just fine. However, now I have got an other problem: One of my other fragments uses a special landscape layout. The problem is, that this layout is not used immediately on device rotation. I think it is because the new layout only gets set on onCreate.
What can I do to solve this problem? I want my landscape layout to be set immediately.
You can put
setRetainInstance(true);
in onCreateView(); method of your Fragment. I think it should do the trick.
As far as I know you down need to add the configChanges parameter to your manifest.
You can override onSaveInstanceState() in your Fragment
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt(KEY_INDEX, someIntValue);
}
This methode should be called before your fragment gets destroyed.
Now in your onCreate(Bundle savedInstanceState) (or onCreateView()) methode:
if (savedInstanceState != null) {
someIntValue = savedInstanceState.getInt(KEY_INDEX);
}
This way it shouldn't intervene with any other special fragments.
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
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 do I retain the state of an activity in android? I have two layouts for portrait and landscape in layout and layout-land. I am loading the value from service at the time I am showing progress dialog. If loaded user rotates the device to landscape at the time also loading. How do I avoid that? user typed content in webview that also refreshed. How do I avoid that, can anybody provide an example?
Thanks
When orientation changes, the Activity is reloaded by default. If you do not want this behavior then add this to the Activity definition in your manifest:
android:configChanges="orientation|keyboardHidden"
For more detail, see Handling Runtime Changes
You can use the onRetainNonConfigurationChange() callback to store arbitrary data. It is called just before your application is about to be recreated.
Then, in onCreate() just check if some data were put aside by calling getLastNonConfigurationInstance() that returns the Object you put aside or null.
See this article on android developers.
Here's a sample borrowed from the link above:
#Override
public Object onRetainNonConfigurationInstance() {
//this is called by the framework when needed
//Just return what you want to save here.
return MyBigObjectThatContainsEverythingIWantToSave;
}
Automagic restore of previously saved state:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final MyDataObject MyBigObjectThatContainsEverythingIWantToSave = (MyDataObject) getLastNonConfigurationInstance();
if (MyBigObjectThatContainsEverythingIWantToSave == null) {
//No saved state
MyBigObjectThatContainsEverythingIWantToSave = loadMyData();
} else {
//State was restored, no need to download again.
}
...
}
When orientation changes, the Activity is reloaded by default. If you do not want this behavior then add this to the Activity definition in your android manifest file :
android:configChanges="orientation|screenSize|keyboardHidden"