How can I force my fragment to redraw the layout? - android

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.

Related

Android: swap width and height on orientation change

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.

Tab Swipe View in landscape, plain layout in portrait

My App is supposed to show a plain LinearLayout with a ListView and TextView when in portrait, but a SwipeView with three tabs when in landscape. The layout is to be changed whenever the orientation of the phone is changed.
I get that normally you'd just have two layout files for portrait and lanndscape but I feel that won't be sufficient in my case. After all the Tab and Swipe functionality requires more than just an XML file. While looking for solutions to my problem it was suggested starting a new activity in the onConfigurationChanged() method, but at the same time this was stated to be very bad practice leading to all sorts of problems.
Now I thought about adding a whole lot of if-else statements to my TagActivity.java always checking what the current orientation is and spawning whatever is needed. Would this be a good way to do it? Is it possible to have multiple onCreate() Methods, one for each orientation?
Any input would be greatly appreciated!
The tutorial I used to implement the TabActivity was the one from androidhive:
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
I did something similar in a recent project. We ended up using a view pager with PagerSlidingTabStrip (https://github.com/astuetz/PagerSlidingTabStrip/issues/5). In portrait only show one fragment in the view pager and hide the tabs, and in landscape populate out the view pager and tabs. Not sure if this works in your particular situation (it seems like it would), but food for thought.

Stubs, Fragments or Compound Controls?

I'm new to Android and a confused about the options to group Views together.
Let's say I want to create a UI where I have 2 sections of controls (one with Buttons and one with text + Spinners) that are above each other in portrait mode and next to each other in landscape mode, and the same goes for the stuff within those sections. Obviously I would like to dynamically change this when the user changes from one mode to another.
So, do I use Fragments within Fragments or use them only the outer section and then a Compound Control for the inner elements? Or are Fragments even necessary and I should better stick to something else? What is the best practice here?
Thank you in advance!
In your example you would create two layout files. One that contained your buttons and the other that contained your spinners.
You would create a 3rd layout file for the portrait orientation and use the include tag to include your other layouts within the 3rd layout. Similarly for your landscape layout, you would include your inner UI layouts in that.
You could then use an Activity or a Fragment which would use only the main layouts and assuming they were placed into the correct layout folders, the fragment/activity would load the correct one based on your orientation.
Fragments within Fragments should be avoided unless you have a specific need for it. It works but in practice managing the lifecycle becomes tiresome.

Android TabActivity and dynamic Tabs screen orientation issue

I am adding Views (not Activities) to a TabActivity on demand (meaning that the activity can have zero or more tabs after onCreate is done). Over the time more tabs get added, now I need to save them in a sane way so I can restore them later on. I tried to use saveHierarchyState etc on the individual views, but they are restored without data from the editTexts etc.
So given that I have a HashMap in the activity which maps from tabkeys to views, how would you save this data and restore it?
Okay, I got it :)
First I thought I needed to set the IDs of the Views as described here -- using this approach Android is able to restore the view states. But since my dynamic tabs all use views from the same layout their children all have the same IDs, so android will overwrite the data (The state is saved in a sparse array according to the view ID, so the IDs should be unique) and all views look the same. I fixed that by manually saving the state of my views and reapplying it via:
SparseArray<Parcelable> container = new SparseArray<Parcelable>();
saveHierarchyState(container);
b.putSparseParcelableArray("state", container);
restoreHierarchyState(b.getSparseParcelableArray("state"));
While this approach works I still have to find out if I can let Android do all the work by not setting IDs for the Views in the layout xml file (if that's allowed/possible).

android -Fragments - onConfigurationChanged -it's possible to change only the layout?

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.

Categories

Resources