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.
Related
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.
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.
I want to make it possible to display a fragment on top of every activity (if the right action is called). It should look like a window on top of everything. This works fine so far, but I am a bit confused about how to get the layout integration working.
When I wanted to display something like a fragment on top of everything, I had to use a RelativeLayout as the root element, then nesting maybe LinearLayout (containing the activity's layout) and fragment.
Is that the only way to achieve this? Do I have to refactor now every activity layout to use RelativeLayout as the base? Or is there a more straightforward way?
I cannot comment, so will write here. Please provide some code to express what you mean, much easier to understand it than. If i understood you correct, u want to have a window on top of another window?
Cannot see why this would be voted down, and not even a comment for the reason :/.
You might achieve what you want with an overlapping fragment. I have had this side effect when making my app, i.e. that a fragment view is transparent on top of another fragment. To achieve this, just skip if(savedInstanceState != null){return;} in your onCreate() in your activity. And instead of replacing a fragment (transaction.replace) use (transaction.add)
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.