I want to rearrange my views in onConfigurationChange when screen orientation changes. Activity restart and views recreation are too consuming; so I have to keep views.
There is one LinearLayout I want to change orientation of. If orientation is changed, I want to swap width and height values of all children elements. (In my case, they can be 0dp, wrap_content and match_parent.)
Of course, I can keep old orientation and iterate over children swapping their width and heights. But this task looks essential and not unique for me. Is there easier way to so that? Maybe it's already implemented somewhere?
It is implemented but only with activity restarts.
One method that doesn't involve iteration would be to recreate the View yourself with the landscape layout which is essentially what the Android system would do. The benefit though is you don't have to recreate the rest of your Activity. The downside is you have to unbind all your listeners that you've applied to your Views (onClickListener, onItemClickListener, etc). Not doing so would create a memory leak.
However, there is a simple method to doing this. Have your layout handled be a single Fragment style that only controls the Views. All clicks and user interaction is handled by this fragment that sends the actions back up to the Activity. When onConfigurationChange is called, simply remove this Fragment from the FragmentManager, then add a new Fragment.
Related
On tablets, one of my activities has a split screen - a list in the left panel, and a map in the right.
If a user wants to maximize the map fragment, is it better to set the left fragment to a 0dp width, or set android:visibility="gone"? If the latter, will it's lifecycle events still be called? (i.e. If the user then exits the application, and re-enters later.)
is it better to set the left fragment to a 0dp width, or set android:visibility="gone"?
In terms of UI load, there is very little difference between these two. Regardless of the option you choose, the left fragment would still be inflated (even though it's not visible to the user). So basically, it's your choice. My preference is usually to alter the visibility (gone/visible) but then, that's just a personal preference.
If the latter, will it's lifecycle events still be called?
Yes. The visibility of a view does not affect/determine its lifecycle.
I hope this helps. Merry coding!
Yes. Its lifecycle events will be triggered. Because fragment lifecycle does not depend on the UI but on the connection to the fragmentManager of an activity.
I am doing a very basic layout inflation of a XML layout into a linear layout container. The inflation is working however when I rotate the view that was inflated disappears (as expected) what I want to know is there a way to save the inflation or view group such as through on save instance state so that I do not need to inflate again since I read layout inflation is expensive!
You shouldn't worry about View inflations being expensive at orientation change! This is how Android works, on orientation change your Activity gets recreated. Period.
But if you really need to (you probably don't), it is possible to force an Activity to not get recreated, in short use android:configChanges="orientation" in your manifest activity declaration. More about this here.
I am using fragments in combination with tabs to display some content. One of those fragments displays data that gets updated with an async task. Since I want to keep the content that was generated on configuration change, I set android:configChanges="orientation" in my manifest file.
This works just fine for the fragment.However, now I have got another problem: One of my other fragments uses a custom landscape layout. This landscape layout is not set on configuration change. I guess it is because I defined in my manifest to handle the configuration change.
Now, how can I force my fragment that uses the custom landscape layout to use the landscape layout on configuration change? What do I have to put in my onConfigurationChanged() method?
I haven't hit this situation myself, so I'm not sure (I've hit it with Activities, and the answer was to call setContentView again, then transfer over the data/state needed). But have you tried deattaching then reattaching the fragment, so it gets to recreate its view? Then you can re-inflate the view, which should inflate in the new mode.
If the differences are minor between the two layouts, another technique is to put both in 1 layout file and fiddle with visibilities on orientation change.
I know you can use this: android:configChanges="orientation" to eliminate redraws on orientation changes. Although it does not in my app. I've read that there may be a method you need to override with it to make it work. But I think that may be overkill.
There are only two issues I do not want to happen when I rotate the screen from portrait to landscape (or the other way).
If the user touches an EditText, keyboard pops up. You shift orientations, and it auto-hides. I want to keep the soft keyboard along for the ride.
I have a ListView loading data populated from a MySQL database. It does this through an AsynTask. When I switch orientations, I do not want this task to be called.
Can I isolate these two issues, or is the first option (configChanges) the answer?
Note: A couple of these are List Activities; but the big one is a FragmentActivity with Viewpager, and a Fragment / ListFRagment (with tabs) inner class inside.
When the orientation changes the Activity is destroyed and recreated. The method you would override if using configChanges is onConfigurationChanged.
For the keyboard question, sounds like you just need to keep a track of it's state and restore it via the Bundle passed in Activity.onCreate.
For the second question, you could check if onSavedInstanceState == null to determine if you need to run the AsyncTask. You will need to save any data to the Bundle in onPause.
I have a question related to fragments, the only solution to change the fragment layout it's if I remove the old instance and replace with new one?
I have two layout's one for portrait and one for landscape - I want to keep all the information's(data) and only replace the layout the onConfigurationChanged() method is called, but I can't force the fragment to recreate the layout.
Thanks, Lorand
I don't know whether you can only change your layout without reloading your data.
My solution is
Don't put android:configuration='orientation'
in the manifest.xml.
When rotating screen, normally onCreateActivity will be call and it'll take the good layout AND RELOAD your data.