What exactly is container parameter in onCreateView method in Fragment - android

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!

Related

Android Fragment container argument in onCreateView

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.

Android - How to access state of checkboxes in fragments from activity

So I have a class which has a tabbed layout of 4 fragments each with a list of checkboxes. I need to save the state of those checkboxes in a sqlite database, but I am not able to access them at all with findViewById from the activity (throws a nullpointerexception). How would I go about doing this? Thanks!
When you first add or replace any fragment.. add a tag for it.. then later use this is acticity
fragment f1 = getfragmentmanager.findfragmentByTag("tag1");
then you can get and save
f1.getCheckbox1().ischecked()...
amd so on...
you can use getFragmentManager.getFragments...
You can't access view elements in your fragment from your activity, it simply wasn't created for this. And why would you need to?
You can simply save the values of your checkboxes inside your fragment code, where you have full access to the entire view. You can still access your sqlite database inside a fragment, it doesn't have to be an activity.
In a fragment you can override the onCreateView and inflate the view.
http://developer.android.com/training/basics/fragments/creating.html
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.article_view, container, false);
TextView textView = (TextView) v.findViewById(R.id.testelement);
return v;
}
And find the elements by doing this.

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.

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();

Do not get the rootLayoutContainer in a Fragment (Android 3.0 Preview)

I'm currently getting into the fragment API of the Android 3.0 Preview and have built the following minimal coding:
I have an Activty, which shall embed Fragment(s), which is currently implemented like this:
public class Cockpit extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cockpit);
}
public static class InfoFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
ViewGroup infoFragmentRoot = (ViewGroup) getActivity().findViewById(
R.id.infoFragmentRoot) ;
return inflater.inflate(R.id.infoFragment, container, false);
}
}
}
The corresponding layout of the activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<fragment android:name="test.android.ui.cockpit.Cockpit$InfoFragment"
android:id="#+id/infoFragment"
android:layout_weight="1"
android:layout_width="10dp"
android:layout_height="match_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="12dp" android:id="#+id/infoFragmentRoot" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
</fragment>
Now, I do not understand why the ViewGroup container in the onCreateView() in the internal class InfoFragment is a nullpointer, nor do I understand,
why
ViewGroup infoFragmentRoot = (ViewGroup) getActivity().findViewById(
R.id.infoFragmentRoot) ;
returns also null.
Thanks for feedback.
You've got a few problems here. First of all, you don't want to be adding tags inside of the <fragment> tag. Think of the fragment tag as a placeholder. The fragment's onCreateView() method is responsible for defining the view hierarchy of your fragment, not the activity's layout XML file. What you can do though is create a separate layout XML file to be just the fragment's layout. Then inside of onCreateView(), you take the inflater passed in, and do something like this:
View v = inflater.inflate(R.layout.frag1, container, false);
TextView text1 = (TextView) v.findViewById(R.id.text1);
text1.setText( myTextData );
return v;
Notice that the attach parameter to inflate() is false? Android will take care of attaching the returned view to your container later.
Your activity's view hierarchy is not guaranteed to be in existence until the fragment gets the onActivityCreated() callback. So the attempt to get infoFragmentRoot could return null inside of onCreateView(). But I'm not even sure what is going on when that tag is buried inside of your <fragment>.
In this particular case, where you've embedded the tag in your activity's layout, the onInflate() callback of your fragment will be called with the rest of the attributes from your tag. The theory is that you could add those attributes to the arguments bundle on your fragment, then retrieve those values later in onCreateView() (using setArguments() and getArguments()). I say in theory because it appears there's a bug in the code that handles a configuration change (e.g., landscape to portrait), resulting in onInflate() being called after onCreateView() when the fragment is being reconstructed after a configuration change. See defect report http://code.google.com/p/android/issues/detail?id=14796.
For now, I recommend you extract your fragment's layout to a separate layout XML file (e.g., frag1.xml), the use my code above to inflate that layout in onCreateView(). And don't worry about any attributes being passed to onInflate().
You also do not want to use onCreate to instantiate your layout, that all will be handled within the parent activity. Saving the bundle is about all ive done there so far

Categories

Resources