Access Activity within Fragment - android

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

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 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 - How the state of views are preserved during Fragment Transforamtion

This confuses me a lot.
I had two fragments, and I use FragmentTransaction.replace() the swap the two fragments. State of some input controls like RadioGroup and checkbox is always preserved when I switch to the other fragments and switch back again.
.
I set break point on my OnCreateView() of Fragment, and every swap seems to trigger the method and new views are created each time. The savedInstanceBundle is always null as well. Since the new views are created, how do they get the state of the old views?
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_fieldsetup_marking, container, false);
// ...
return v;
}
This effect is fine for my app, but I just want to know how it is implemented.
where are the states stored?
Where is the android SDK code to restore this (I can not find any in the class FragmentTransection).

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