Justin James
Justin James

Reputation: 3

What to do in code for android fragments

Hello I am making an app for android which is completely based on fragments. It has a menu and on clicking any item a fragment according to option opens. I am confused how to put the code for the fragment in the class for fragment.I was reading a post here,in which a solution was provided to code the fragment like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_library, container, false);
    return rootView;
}
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // do your variables initialisations here except Views!!!



 }
 public void onViewCreated(View view, Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);
        libraryHeading=(TextView)getView().findViewById(R.id.Library);   

        libraryHeading.setText("Hey I am Library");

        // initialise your views

 }

Now in onViewCreated() I will initialize views,in onCreate() I will initialize my variables but I am not getting what will I do in onCreateView()? Please help me.I am new to android development.If I am doing something wrong please tell me.

Upvotes: 0

Views: 91

Answers (3)

PPD
PPD

Reputation: 5890

As per docs

public void onViewCreated (View view, Bundle savedInstanceState)


Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle) has returned, but before any saved state has been restored in to the view. This gives subclasses a chance to initialize themselves once they know their view hierarchy has been completely created. The fragment's view hierarchy is not however attached to its parent at this point.

If you are accessing the view hierarchy of the parent activity then you must be done it in the onActivityCreated, not before it.

Upvotes: 0

Justin James
Justin James

Reputation: 3

Ok thank you very much.I understood it.It was happening due to this line:

libraryHeading=(TextView)getView().findViewById(R.id.Library);

I should have to use rootView instead of getView() method

Upvotes: 0

Alex K
Alex K

Reputation: 8338

I'm not quite sure what guide you're following.

In my applications, I do all of the view initialization in the onCreateView() method. So something like this:

TextView sampleTextView;
ImageView sampleImageView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_xml_layout, container, false);

    sampleTextView = rootView.findViewById(R.id.sampleTextView);
    sampleImageView = rootView.findViewById(R.id.sampleImageView);

    sampleTextView.setText("You can do all initialization here...");

    sampleImageView.setBackground(Color.WHITE);

    return rootView;
}
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // do your variables initializations here except Views
}

You can forget the whole onViewCreated() thing, at least for now. Just use the onCreateView() method the way I did above.

Upvotes: 2

Related Questions