Changing the appearance of a fragment - android

Is it possible to change the appearance of a fragment (set a different View) during its operation or is it possible only in the onCreateView () method?

You can possibly do it this way:
Return some placeholder view (e.g. FrameLayout) in your onCreateView() and keep reference to it.
Then, you can add/remove other views from your placeholder view, when you need to do so.
However, I think it is not how Fragments are supposed to be used. (changing its views rapidly during runtime)

I'll answer my own question.
Not to create chaos in the management of widgets. It is better to use child fragments or show/hide necessary widgets. The answer found here.

Related

What are the advantages of adding a fragment via XML vs programmatically?

From the Android documentation it's not very clear to me which are the advantages and practical use cases of adding fragments via XML compared to adding them programmatically.
Do both methods allow sending data from the activity to the fragment and back using Bundle?
Can both methods behave similarly in the activity lifecycle?
Some short examples or references will surely help.
Both the methods have only slight difference. If you add fragment in XML, then you are first loading or creating it then you will get its instance and vice-versa for the other approach.
Also, programmatically adding fragment gives you advantage of changing its attributes dynamically whereas you have fixed values if you add it from XML.
Not that much major difference it has.
With FragmentContainerView and using the android:name or android:class, you can avoid the boiler plate code of instantiating the fragment only when savedInstanceState is null or when it is not already added.
If you do that programmatically, you need to make sure that you only add the fragment if it is not already added to the activity by checking:
if (getSupportFragmentManager().findFragmentByTag(CUSTOM_TAG) != null)
{
// You can also easily add animations or pass custom data.
getSupportFragmentManager().beginTransaction().add(R.id.container_view, YourFragment.newInstance(data), CUSTOM_TAG).commit();
}
Programmatically doing it gives you advantages of passing custom data, adding it when you actually need it. In case of layout method, it will be instantiated when the activity's layout gets inflated. But many times, we don't need to add a fragment immediately, adding it programmatically would be a better option in that case.
No difference at all. Android has a system for instantiating objects from XML, but it's always interchangeable with actually executing constructors and the methods to add children. The difference is convenience: The XML system allows you to easily link other resources and it has features to help with passing the right parameters.

Adding Fragment as a List Item

I am having a situation in which as a List Item I want to inflate a Fragment, but It seems like a bad approach. As Fragments are processed/managed by Activity's FragmentManager or by child FragmentManager and list item views are by ListView & ListAdapter.
Any comments and research regarding this would be highly appreciated.
Thanks
Here are my views and questions in mind on your problem.
You want to use ListView with fragments, since you already have a fragment which does that job and you dont want code to become redundant.
Though you can definitely use fragment, but i suppose its not the best practice. Fragments have their own life cycle and you are not going to use fragment life cycle methods (I suppose). Thus semantically it would not fit into this usecase.
And also your adapter will always be dependent on activity to retrieve fragments. (could there be any problems with orientation change again?)
List items and adapters are finetuned to work really well with scrolling really long lists. While the list view items get recycled when using view holder pattern, while scrolling, does any of fragment lifecycle methods come in between? would that cause performance impact. (I suppose yes. Havent tested it out yet)
You can instead have your view code in different layout file and include this layout in both fragment and also list adapter.
<include layout="#layout/YOUR_COMMON_VIEW_CODE"/>
and have utility class which takes the context and this layout container. Have all the functionality exposed inside that utility class.
You can't use fragment as list item views because the API doesn't allow you - View and Fragment aren't even related so there's no way you can use it like that. Make custom views and use adapter getViewTypeCount and getView to use different list item behavior.
Fragment are managed by Activity's FragmentManager or by other Fragments child FragmentManager; while list item views are managed by ListView & ListAdapter. You can use ListViews in Fragments, but not the other way around.
I googled and got this.

Assistance needed Understanding Android fragment addition during onCreate

I just need help cleaning up some information that doesn't make sense to me, with use cases if possible.
WHAT I UNDERSTAND:
With fragments in android, I understand that if you plan on replacing them you need to have a container view, preferably a FrameLayout, and add the initial fragment to the container during the activities onCreate method. But there is one thing that continues to not make sense to me.
WHAT I DON'T UNDERSTAND AND NEED HELP WITH:
What rules are there regarding where/how the container view is set up, if there are any. Android Developers site makes it look like the container view needs to be it's own XML layout file, but it doesn't say that and I have seen examples on here with FrameLayouts nested inside of your typical layout files, but they are all specific uses and I need to understand the rules of setting a container up.
There are no rules. You just need any ViewGroup -- position and size it however you want. When you add the Fragment into it, it will behave just as if you'd created the Fragment's View manually, and called yourViewGroup.addView(fragmentView).
FrameLayout is typically used just because it makes a good container with no real behavior (you just give it a size and position and let the fragment fill that container).
There's absolutely no need to make the container view its own layout file. In fact, if you want the Fragment to take over the content view of the entire Activity, you could even just add the Fragment to the Activity using the ID android.R.id.content.

Nested fragment lifecycle

I have 4 fragments. I switch among them by using hide/show transactions. One of them may have nested fragments which are added by replace transaction. The issue is that if I add a nested fragment hide main fragment and show it again the onHiddenChanged method of my nested fragment is not called. Why so? How can I determine when my nested fragment become visible?
I also came across the problem you are facing.
I found that hiding/showing a parent fragment had no effect on the child fragments' visible states, and wouldn't call onHiddenChanged() and would return isVisible() = true always.
See my answer in another question for my workaround.
Basically I created my own getIsVisible() function on a BaseFragment which recursively looks through any parents to find it's actual visible state. I am sure you could do something similar with onHiddenChanged() by propagating it to all the child fragments.
I know this is old, but maybe someone could benefit from this knowledge :)
For inner fragments, use getChildFragmentManager().

Conditionally switch between layouts in Android

How do you switch between layouts in an activity and populate fields based on layout currently used?
For example if there is a logic in a view to load a view:
if(category == 1){
setContentView(R.layout.layout1);
}else{
setContentView(R.layout.layout2);
TextView title = (TextView) findViewById(R.id.titleTV);
title.setText("myTitle");
}
If the else code is called the view never sets the title TextView to the String.
How do I accomplish conditionally going between the two views?
Another alternative would be to use Fragment. The advantage over the choices dimitris mentioned is that each fragment has it's own lifecycle, so you can delegate all code related to each view to its fragment, and keep your main activity clean.
To do this, simply use a FrameLayout in the Activity as a placeholder for the fragments. Fragments can communicate with the activity by using the listener pattern.
In case you want to flip across difference Views you can examine various possibilities, such as:
ViewPager via the compatibility library (http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html)
ViewFlipper or ViewSwitcher (http://developer.android.com/reference/android/widget/ViewFlipper.html, http://developer.android.com/reference/android/widget/ViewSwitcher.html)
Or even a simple FrameLayout (http://developer.android.com/reference/android/widget/FrameLayout.html)
Hope this helps for now! In case you need anything specific please shoot it!
BTW have you considered Fragments. They are meant to deal with conditional layouts. Their basic motivation is screen sizes etc. But they can also fit your problem.
Refer to:
Fragments

Categories

Resources