Android Fragment container argument in onCreateView - android

I wonder where container argument in onCreateView() method of Flagment come from , is that a ViewGroup of the activity which has this fragment or something else ? I wonder it cause i don't know what ViewGroup that xml of fragment attract to in this code .thanks for your time
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_list,container,true);
}

The container parameter passed to onCreateView() is the parent ViewGroup (from the activity's layout) in which your fragment layout will be inserted.
Rest you can refer to this link

Not sure why this question was down voted. IMO it's a good question trying to understand what the framework is giving us from its callbacks.
I have the same question, and after reading some official docs, here is my theory:
To make it more specific, I think the container is the FragmentContainerView in this example. Basically it's the resource id which you add your fragment to. For example, if we do
fragmentTransaction.add(R.id.container_view, fragment).commitNow();
Then the container is the ViewGroup identified by R.id.container_view.

Related

What exactly is container parameter in onCreateView method in Fragment

When I want to create a simple fragment, usually use following code:
public class TestFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_latout, container, false);
}
}
My questions are:
What exactly is container parameter? where it is defined?
Can we see this ViewGroup(container) in XML/Layout Designer?
In this method(onCreateView), what is this ViewGroup(container) id?
In R.id.x, to access this ViewGroup(container) What should we replace x with?
Thanks.
You don't call that method, the system does, as part of the fragment lifecycle.
When a fragment is added to a layout, onCreateView will eventually get called, and container is the view that holds the fragment. That lets you do things like access the parent's layout parameters, the style/theme applied to it, etc. That's why you pass container when you call inflate, so it can apply any necessary attributes while inflating the fragment's layout.
Basically don't worry about it, and pass the parent/container when you inflate a layout. 99% of the time, that's all you need to know!

Android Development, how does this line of code become produced?

i'm reading this android dev book and im stuck understanding how this line of code is error free (please keep in mind i've gotten rid of some code because for more focus on this part.
public View onCreateView(LayoutInflater layoutToInflate, ViewGroup parent, Bundle saveState)
{
View v = layoutToInflate.inflate(R.layout.activity_main_fragment,parent,false);
return v;
}
from what i believe, i need a method that returns a view because the Class extends from a Fragment class, not an activity so i have to explicitly find the view, the parameters are straight forward what i dont understand is how we create a view and set it equal to layoutToInflate...false;
I think you have a misunderstanding what the concepts of Fragments are. They reside inside an Activity. If a Fragment has a UI, it needs the parent Activity to also have a UI. That also means Fragments have a ViewParentbelonging to an Activity. This parent is given to the Fragment by the ViewGroup parent argument. So when creating a Fragment with a UI, you need to inflate the layout belonging to your Fragment and pass it to the Activity, which adds it to the ViewGroup parent. So that's why you get a LayoutInflater to inflate your Fragment's view:
View v = layoutToInflate.inflate(R.layout.activity_main_fragment,parent,false);
Afterwards you return it to give it to the parent Activity.
layoutToInflate is a variable of LayoutInflater and R.layout.activity_main_fragment is the name of the layout file to be inflated.

How to check if view was already loaded? (InflateException: Duplicate id)

I'm building an android app in which I use one Activity from which I can load several fragments. So I start with the FragmentA, and from that I load Fragment B as follows:
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.content_frame, detailsFragment).addToBackStack(null).commit();
This works fine. When I press the backbutton, it also goes back to FragmentA perfectly well. The problem is that when I click on the button to go to FragmentB again I get an InflateException (Duplicate id 0x7f08007a, tag null, or parent id 0x0 with another fragment for com.ourcomp.fragment.DetailsFragment) on this line:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_details, null);
}
I think I need to check whether it was inflated already, but I don't really know how. Does anybody know how I can check wether the view was already inflated so that I can prevent this error?
After checking documentation again, I found:
The ViewGroup to be the parent of the inflated layout. Passing the container is important in order for the system to apply layout parameters to the root view of the inflated layout, specified by the parent view in which it's going.
So try to replace
return inflater.inflate(R.layout.fragment_details, null);
with
return inflater.inflate(R.layout.fragment_details, container);

Access Activity within Fragment

Im making an App. And I want to access my 3 main activities into 3 different fragments. i use viewpager to swap activities but it only show layouts do not work on its functionality. Then i used Fragment to use functions. but my problem is same its only show layouts not is functions.
i made 3 different fragment for 3 activities and use
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.settings, container, false);
return v;
}
method.
idont know how to use onActivityCreated() method or getActivity(). please help me..
i search all of it about it but cannot done yet.. :(
To access Widget Of layout just use this
View.findViewById();

More instances of fragment at one time - resources referencing to wrong views in android

this is a though one for me. I have a MainActiviy which extends FragmentAnctivity. There I have 1 FrameLayout and buttons below to change frame's content. I do so by switching show/hide for created fragments which I added to FrameLayout before in OnCreate.
I'm also nesting more fragments in 1 fragment (As I have 1 fragment for 1 type of content and inside of it there is listFragment which is changed to DetailFragment after OnItemClick... again with show/hide approach).
Problem is that in 2 different contents I have 2 different instances of 1 Fragment class, so those 2 instances use 1 same layout file. And although the first of those fragment is hidden and 2nd is shown, when I change some view through 2nd instance then layout of 1st instance is changed and 2nd remains same as before. (Hope it is understandable)
I guess it's totally a mistake in managing and understanding of fragments' lifecycle, so can please someone help me to solve this?
Thanks very much :)
I suppose you get main point of fragments using practices. Your problem is simple. I almost sure you use getActivity().findViewById(...) calls to access views in your Fragment (or nested Fragment whatever). I this case Activity would return you fist view with defined id from whole your views hierarchy.
Solution is pretty simple - you just must avoid getActivity().findViewById(...) construction and get all links to views in onCreateView() callback and use exact this link with all future operations. Than everything will be ok. Here is simple example:
private TextView mDummyText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.layout_name, container, false);
initMembersViews(v);
return v;
}
private void initMembersViews(View v) {
mDummyText = (TextView) v.findViewById(R.id.fr_houses_list_text);
}
Hope it would helps you! Good luck!

Categories

Resources