简体   繁体   中英

When to use onCreateView for fragments?

I'm following steps on

http://developer.android.com/training/basics/fragments/creating.html#AddInLayout

and I can't seem to figure out why they stated, "One difference when creating a Fragment is that you must use the onCreateView() callback to define the layout." but didn't use onCreateView for the HeadlinesFragment, only the ArticleFragment. They both seem to set a layout so I don't understand why onCreateView is not used. Also, is the fragment_container only the xml file? I'm guessing it's only used in the MainActivity. Code is posted on

Android Fragment Basics Tutorial

except code for articleFragment which I posted below. So far, I think I understand that you have a class that extends fragmentactivity that holds fragment container and methods to switch out other fragments. Then you have the fragments that are classes that extends fragment or listFragment? However, the site,

http://developer.samsung.com/android/technical-docs/Using-Fragments-to-Build-User-Interfaces-in-Android

shows examples of activities that DONT extend fragment activity but rather just activity and it's supposed to hold the other fragment activities.

This site:

http://developer.android.com/reference/android/app/Fragment.html

showed more information on fragments but the lifecycle doesn't clarify when to use OnCreate vs onCreateView, I'm thinking it's something in regards to the layouts but it states that onCreate starts fragment too so I'm not positive on which to use.

THANKS!!!!!

package com.example.android.fragments;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ArticleFragment extends Fragment {
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;

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

    // If activity recreated (such as from screen rotate), restore
    // the previous article selection set by onSaveInstanceState().
    // This is primarily necessary when in the two-pane layout.
    if (savedInstanceState != null) {
        mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
    }

    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.article_view, container, false);
}

@Override
public void onStart() {
    super.onStart();

    // During startup, check if there are arguments passed to the fragment.
    // onStart is a good place to do this because the layout has already been
    // applied to the fragment at this point so we can safely call the method
    // below that sets the article text.
    Bundle args = getArguments();
    if (args != null) {
        // Set article based on argument passed in
        updateArticleView(args.getInt(ARG_POSITION));
    } else if (mCurrentPosition != -1) {
        // Set article based on saved instance state defined during onCreateView
        updateArticleView(mCurrentPosition);
    }
}

public void updateArticleView(int position) {
    TextView article = (TextView) getActivity().findViewById(R.id.article_fragment);
    article.setText(Ipsum.Articles[position]);
    mCurrentPosition = position;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    // Save the current article selection in case we need to recreate the fragment
    outState.putInt(ARG_POSITION, mCurrentPosition);
} 
} 

If you look closely at the code you will see a difference between HeadlinesFragment and ArticleFragment :

public class HeadlinesFragment extends ListFragment

public class ArticleFragment extends Fragment

The difference of course is that HeadlinesFragment is a ListFragment rather than just a plain ol' Fragment . ListFragment already implements a default onCreateView() that loads a layout with a ListView . If you want to override the default behavior, you can write your own onCreateView() method to make a more sophisticated layout.

I learned that since the fragment lifecycle is tied to the activity onCreate creates both the activity and the fragment. onCreate is used to set activity layout where onCreateView sets the fragment layout.

the reason headlinesFragment didn't have onCreateView is because since it's a list it has a predefined layout and thus doesn't need to be overridden.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM