android:configChanges="orientation|screenSize" changes my layout look - android

I have an Adapter class and a MainActivity. The Adapter class displays the list of children names and beside the names, it displays the time they arrived to the school. Problem is when I rotate to landscape, the time values are lost.
I considered adding in my Manifest under MainActivity
android:configChanges="orientation|keyboardHidden|screenSize"
What happens is that the time values are not lost in landscape mode but the look of the layout appears same as that of portrait mode. In real, the landscape layout looks a bit different from portrait layout.
What do I do in this case in order to obtain time values and also maintain the look of the landscape layout.

There are 2 options:
use onSaveInstanceState(Bundle outState) method in your Activity or Fragment - to save your data and restore them after rotation (in onCreate(Bundle savedInstanceState) or onRestoreInstanceState(Bundle savedInstanceState))
Example:
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("time_data", (Seriazable) mTimeList);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if (savedInstanceState != null) {
// restore value of members from saved state
mTimeList = savedInstanceState.getSerializable("time_data");
}
...
}
use android:configChanges as you already use to handle changes by yourself but inflate landscape layout after rotation
Example:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
I prefer the first option and also this proposed in Android guide.

you can simply set screen orientation to portrait to your perticular activity so it cant be rotate. (if you want)
like this way:
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
</activity>

Related

Android Rotation (Load Layout) data loss (id and data / Custom Objects etc.)

I am turning a view, which is the same but horizontally. which contains many variables of type TextView, progressbar, imageview, etc .. but I can not load the variables when rotated.
I tried to do:
android: configChanges = "keyboardHidden | orientation | ScreenSize | ScreenLayout"
I find methods that can save my customized classes, my progressbar, my views "My nothing."
http://i.imgur.com/A1ugd3W.png
When there anyway to load a landscape (another layout (with the same id's)) of not losing data?
To change the layout I use:
http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration)
Update Code 10/04/2015 (0:45 AM)
My rotation :
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_1);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_2);
}
}
Oncreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_1);
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
setContentView(R.layout.activity_2);
}
References : Saving Android Activity state using Save Instance State Can't sabe all objects exits of an activity. "I think"
The easiest way would be to not specify orientation for the configChanges. Correspondingly, you will not need to override onConfigurationChanged -- you can simply load the layout in onCreate and state should for the most part be preserved. If needed state data can be stored in onSaveInstanceState (and restored in onCreate).
can you provide your code?
One possible issue is that you might be loading your layout file in onResume instead of onCreate. You need to create the layout in onCreate so the savedInstanceState info gets "injected" back into your app. But again, please provide codes :)
My guess is that elements of your layout don't have the same ID. Also, why don't you just have an XML for certain specifications - one for landscape and one for portrait? I bet Android would behave better if you did that instead of a different name for a different configuration

save value of a variable in a DialogFragment when the screen rotates

Well, I wanted to save value of a variable contained in a dialogFragment when the screen is rotated in Android. I've tried every method I could find on the internet, and none of them has worked for me. Some kill my application, and others simply were not doing anything.
I need a real and effective way to save the value of an EditText that is reset when the device screen rotates. The EditText is in a DialogFragment turn this into a FragmentActivity.
thank you very much
Set your fragment's retaingInstance flag to true:
http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)
This prevents Fragment instance from being recreated.
Also be sure you don't recreate the fragment all the time:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
if (savedInstanceState == null) {
Fragment f = new Myfragment();
f.setRetainInstance(true);
getSupportFragmentManager().beginTransaction()
.add(R.id.container, f))
.commit();
}
}
Since View state is preserved during Activity recreation and your are keeping the same Fragment instance you don't need to save TextView value all the time.
I don't know if I understood your question, but you can capture the rotation as follows:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
//save value
}
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//save value
}
}
If you want that your activity don't restart, you should add this into the manifest file:
<activity
.....
android:configChanges="keyboardHidden|orientation|screenSize"
..... >
</activity>
I just found out that you can not apply the methods to save bundles automatically to the dialogues, but these would apply to activities or fragments of these dialogues dependent. If I can fix it by code, will put the solution here. thank you

After orientation switch, TextView is no longer updating

I have an Activity with a TextView, created in the usual findViewById method, using the definition from my xml layout file.
I'm updating the text in the TextView using a handler, created in the onCreate() method.
When I switch orientation, the TextView no longer updates from the same Handler.
If my xml definition states a value for android:text="blabla", then I see "blabla" in the view, but cannot update it anymore.
What's happening?!
When u switch orientation, android create new view.
I'd suggest making that mHandler volatile.
If u decide just to use only one orientation, for landscape add android:screenOrientation in activity tag in android manifest
The reason why you cannot update anymore is because Activity reference is different.
Your first created Activity 'A', has handler 'a'.
Now handler 'a' only can update view that belong Activity 'A'.
So when orientation happened, Activity 'A' is destroyed and Activity 'B' and handler 'b' is created.
So 'a' cannot update 'B's view in front of screen.
For quick solution I recommend you disable re-create activity when mobile orientation rotated.
AndroidManifest.xml
<activityandroid:name=".MyActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name">
Activity
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if(newConfig.orientation ==Configuration.ORIENTATION_LANDSCAPE){
Toast.makeText(this,"landscape",Toast.LENGTH_SHORT).show();
}elseif(newConfig.orientation ==Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this,"portrait",Toast.LENGTH_SHORT).show();
}
}
now onCreate will not be called when orientation.
Try using,
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//your code
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//your code
}
}

TabGroupActivity resume wrong view when orientation change

I have a application that use TabGroupActivity, when I change the screen orientation they resume the first view I have loaded. Example: I'm on the view 3, I rotate the phone to landscape and the first view (the loading view of the tabGroup) was resumed.
How I can resolve this?
Thanks
You should save which tab you're on in onSaveInstanceState():
public void onSaveInstanceState(Bundle toSave) {
super.onSaveInstanceState(toSave);
toSave.putString("currentTab", tabs.getCurrentTabTag());
}
Then restore it in onCreate():
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
String tabTag = null;
if (savedState != null) {
tabTag = savedState.getString("currentTab");
}
... //tab initialization code
tabs.setCurrentTabByTag(tabTag);
...
}
If you have load your view onCreate(), then try the following with your TabGroupActivity, in Manifest file:
<activity
android:label="#string/app_name"
android:name=".TabGroupActivity"
android:configChanges="orientation" />
In the manifest for that activity set the android:configChanges attribute. You should set the value to "orientation | keyboard | keyboardHidden", this will stop your activity from being killed and restarted thus keeping your current state.

onConfigurationChanged-it's still showing the first layout

I have an activity which handles configuration changes. But now i have to change
layout.I tried in onConfigurationChanged callback just to set again the layout and hoping will get the correct layout(land) but it's still showing
the first layout for portrait view ( there two layout(same name) are placed in res/
layout and res/layout-land :)
if I delete android:configChanges="orientation", it works should be, but ı need to handle onConfigurationChanged. What should I do??
If you have your portrait layout main.xml in /res/layout and your landscape layout main.xml in /res/layout-land, and your onConfigurationChanged() looks like this:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
...
...
...
}
And in your Manifest you should have android:configChanges="keyboardHidden|orientation"
Then it should work fine, as this does in my app. Is this what you are doing?
// In your activity code.
int mCurrentOrientation;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mfo_offers);
........
mCurrentOrientation = getCurrentOrientation();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// You may handle different configuration changes in your activity which configured in your mainfest.xml, you only need to recreate layout when orientation changes.
if(mCurrentOrientation != newConfig.orientation) {
recreate(); // This recreate the activity if you can properly restore your activity state.
}
......
}
See Activity.recreate() here: http://developer.android.com/reference/android/app/Activity.html#recreate()

Categories

Resources