mercredi 2 juillet 2014

When to use onCreateView for fragments?


Vote count:

0




I'm following steps on


http://ift.tt/1iXyQXw


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? I couldn't find information on why onCreateView was not used, what HeadlinesFragment is or the complete list of classes I can extend for making fragments (not the fragment container). However, the site,


http://ift.tt/1jmwHxy


shows examples of activities that DONT extend fragment activity but rather just activity and it's supposed to hold the other fragment activities. If I have activity1 and fragment A and Fragment B, what will each extend assuming that activity1 will hold and switch fragmentsA and B? Does it matter if before or after runtime in regards to what class I'm extending?


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


asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire