All inflated items are vanished when screen orientation changes [duplicate] - android

This question already has an answer here:
Inflated items are gone when device orientation changes
(1 answer)
Closed 5 months ago.
Please somebody help me..
In my activity i use a loop to inflate items using LayoutInflator Service. There are some TextViews also in my activity. The TextView values remain unchanged but all the inflated items are vanished when i change the screen Orientation..
I know that the activity is recreated again to redraw the UI to match the layouts and all so I tried saving instance to a bundle. But helpless. Can i save these Inflated Items to that bundle and restore it on restore.
One guy told me to hide the soft keyboard when you change the orientation. I set properties on the manifest and also hiding keyboard on the orientation change event listener.
The only way now i can protect my app is to turn off the auto orientation. But thats not what i want.

In the manifest, put android:configChanges = "orientation" for this activity. This will prevent the activity from being destroyed and recreated when you rotate the screen.
Note that you have to be very careful when doing this. If any of your resources depend on the orientation configuration (like if you have a resource folder called values-land or drawable-port, etc.) then these resources must be reloaded after the configuration change. See documentation here.

Related

How to enable rotation on android on same activity?

I am sorry, this question has been asked probably thousand times over, but I do not find the correct words how to ask this question.
I have an activity in android. When I turn the screen at 90 degrees, it looks like the current activity is completely destroyed and a new activity is initiated. Is that correct?
But if I have showed some data in one orientation, I want to show the exact same data in the other orientation? Is there some SIMPLE way to achieve this? That the activity just remains, but shows the layout with the SAME data from the SAME activity in the other orientation?
there is an easy way to save your time, if your project api level is upper than Android 3.2 (API level 13) add this code into your activity in manifest:
android:configChanges="keyboardHidden|orientation"
Update: this also can help:
android:configChanges="keyboardHidden|orientation|screenSize"
Yes whenever an activity changes its rotation it is destroyed and recreated. and the same android uses same layout and roughly shows it into landscape mode.
You can make a new folder layout folders name layout-land.
layout-land may contain the same layout as in layout folder and the views can be tweeked according to the landscape orientation requirments
Another way would be to store all your information in savedInstanceState and restore it from there in onCreate(). Or use a Fragment with setRetainInstance(true) to store your information in.

Why current tab changes on screen orientation change Android

I would like to know why when screen orientation is changed current tab changes back to default one in TabHost?
I understand that the Activity is destroyed and created again, but why the state of TabHost isn't saved? Per example, text of an EditText is saved and restored, why is it different for current tab? Do I have to do it myself?
Thanks
You are responsible for managing your own tabs ( fragment transactions). So it follows that if you want a particular tab to be selected then you have to save state information prior to the configuration change.
This may help (especially if you follow the link in the answer):
How can I prevent the current tab view from being lost when rotating the screen?
By default, EditText saves its own instance. See Yalla T's answer here:
How to retain EditText data on orientation change?

Orientation Change - Update UI

I use data of the accelerometer in my app. If the device´s angle is changed the screen orientation should change.
In order to change the orientation I have 2 layouts, one for portrait and one for landscape.
When the orientation changes a function is called that changes the layout with:
setContentView(R.layout.landscape); or setContentView(R.layout.portrait);
This works fine but I have a problem with several UI elements like buttons or ToggleButtons.
I initialize a onClickListener in the onCreate Method for them and each time the orienation gets changed I initialize a new onClickListener.
Unfortunately I can´t change the state of ToggleButtons anymore.
How can I solve this problem?
you can use /layout-land and /layout-port and the phone will automatically switch them for you on orientation change. see
http://developer.android.com/guide/practices/screens_support.html#DesigningResources
as for the toggle buttons, see onRetainNonConfigurationInstance()
http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance%28%29
As described by Bill Gary above, why not just let the system take care of orientation changes for you? Also the standard system widgets should automatically preserve their state when you do this. See here for why and how to manage custom state, if you need to.

Android save View position on orientation changed [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Android - disable landscape mode?
I can't save position of view when orientation changes.
I think it's because with orientation it also changes and I want that view stayed on the same position. For example of it was in the upper left corner of screen it stayed there and it doesn't matter what orientation of the device.
I'm using some kind of Absolute layout. Because I need to manage few screens at the same time.
So, i have x/y coordinate system. I think it's the main problem, but I can't solve it :(
I've attached images that you could see my problem.
As you can see first screen is portrait mode and View is located in upper-left corner, but when I changed to landscape mode position of view is also changed to upper-right corner.
And I need that position of view stayed always in left-upper corner.
http://imgur.com/qo19E&u2K0hl
http://imgur.com/qo19El&u2K0h
Sorry to bad screen. I'm still developing of an application and can't provide better screens :)
Ps: If you need some code, just say what part of code do you need.
Duplicate of Android - disable landscape mode?
You can force the activity to run in a particular orientation.
Inside the AndroidManifest.xml use the attribute "android:screenOrientation"
See here for an example:
http://developer.android.com/guide/topics/manifest/activity-element.html
It would be nice if you could provide some code or screenshots.
Anyway, when the orientation changes, your activity is destroyed and recreated.
If you have a well designed xml layout file (using linearlayout, relativelayout and properties like "wrap_content", "fill_parent"...) it will reload it in the onCreate() method, so all the resizing should be done automatically.
If your views can be moved by the user or are loaded from runtime values, you can save it. Look at this post for exemple : Override Orientation Change But NOT Restart The Activity AND Pass State Data

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.

Categories

Resources