PAcan PAcan - 1 year ago 122
Android Question

Why does the fragment's onCreateView, onCreate, onActivityCreated are called

I have an app, that deals with fragments and ViewPager. I have three fragments in a ViewPager. When you switch between them, it always causes the other two fragments to call their's onCreateView methods. How to do it only once, only when FragmentActivity is created???
I've read some questions and tried the solutions, but the fragments still have the same behavior.

ListFragment onCreate called twice

onCreate() and onCreateView() invokes a lot more than required (Fragments)

Here is some code, if it helps you, guys:


public class StartingActivity extends FragmentActivity implements View.OnClickListener {
ViewPager viewPager;
CirclePageIndicator pageIndicator;

Button discount;
Button qrCode;
Button pay;
TabHost tabHost;

public void onCreate(Bundle savedInstanceState) {
viewPager = (ViewPager) findViewById(;

if (savedInstanceState == null) {

Fragment firstPage = Fragment.instantiate(this, FindTovarFragment.class.getName());
Fragment secondPage = Fragment.instantiate(this, MainWindowActivity.class.getName());
Fragment thirdPage = Fragment.instantiate(this, MapActivity.class.getName());

if ((firstPage != null && !firstPage.isDetached())|| (secondPage != null && !secondPage.isDetached()) || (thirdPage != null && !thirdPage.isDetached())) {

List<Fragment> viewPagerFragments = new ArrayList<Fragment>();

PageAdapter pageAdapter = new PageAdapter(getSupportFragmentManager(), viewPagerFragments);


pageIndicator = (CirclePageIndicator) findViewById(;
pageIndicator.setCurrentItem(pageAdapter.getCount() - 2);



public class MapActivity extends Fragment implements OnMyLocationListener {

//Тэг для логов
private static final String TAG = "MapActivity";
List<Address> addressList;
private static final String STRING_LOCATION = "";

ArrayList<TorgCentr> randomTorgCentr;
ArrayList<String> torgCentrNames;

Context context;
AutoCompleteTextView searchTorgCentr;
OverlayManager overlayManager;
MapController mapController;
TextView textView;
double longitude;
double latitude;
double itemLongitude;
double itemLatitude;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "MapActivity onCreateView");

View view = (LinearLayout) inflater.inflate(R.layout.map_layout, container, false);
final MapView mapView = (MapView) view.findViewById(;
textView = (TextView) view.findViewById(;
searchTorgCentr = (AutoCompleteTextView) view.findViewById(;

mapController = mapView.getMapController();
context = getActivity();
return view;

public void onResume() {

public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "MapActivity onCreate");


public void onActivityCreated(Bundle savedInstanceState) {
Log.d(TAG, "MapActivity onActivityCreated");
context = getActivity();

SetRightMapDisplayAddress rightMapDisplayAddress = new SetRightMapDisplayAddress();

DownloadSuperMarketsArray superMarketsArray = new DownloadSuperMarketsArray();

overlayManager = mapController.getOverlayManager();


Second Fragment:

public class MainWindowActivity extends Fragment {

private static final String TAG = "MainWindowActivity";

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "MainWindowActivity onCreateView");
View view = (RelativeLayout) inflater.inflate(R.layout.main_window_layout, container, false);
if (container == null) {
return null;
return view;

And the third one:

public class FindTovarFragment extends Fragment {

private static final String TAG= "FindTovarFragment";

Context context;
ArrayList<Category> categories;
Spinner categoryContainer;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "FindTovarFragment onCreateView");

View view = (LinearLayout) inflater.inflate(R.layout.find_tovar_main_layout, container, false);
categoryContainer = (Spinner) view.findViewById(;

return view;

public void onActivityCreated(Bundle savedInstanceState) {
Log.d(TAG, "FindTovarFragment onActivityCreated");
DownloadCategory downloadCategory = new DownloadCategory();

Logs for MapActivity:

06-20 11:06:37.709: DEBUG/MapActivity(1290): MapActivity onCreate
06-20 11:06:37.709: DEBUG/MapActivity(1290): MapActivity onCreateView
06-20 11:06:38.509: DEBUG/MapActivity(1290): MapActivity onActivityCreated

Then again and again:

06-20 11:07:53.239: DEBUG/MapActivity(1290): MapActivity onCreate
06-20 11:07:53.239: DEBUG/MapActivity(1290): MapActivity onCreateView
06-20 11:07:53.429: DEBUG/MapActivity(1290): MapActivity onActivityCreated
06-20 11:08:23.029: DEBUG/MapActivity(1290): MapActivity onCreate
06-20 11:08:23.039: DEBUG/MapActivity(1290): MapActivity onCreateView
06-20 11:08:23.269: DEBUG/MapActivity(1290): MapActivity onActivityCreated

Thank you very much in advance.

Answer Source

I would change your architecture for this one on the android developer documentation:

but I would change some things...

1-I would change this method:

     * The Fragment's UI is just a simple text view showing its
     * instance number.
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
        View tv = v.findViewById(;
        ((TextView)tv).setText("Fragment #" + mNum);
        return v;

For something like this where we decide which fragment you populate depending the position of the viewPager:

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);

SupportFragmentManager ft = getChildFragmentManager().beginTransaction();

String tag = "";
Fragment fragment = null;

  switch (mNum) {
  case 0:
    fragment = new MyFragmentZero();
    tag = FragmentTags.TAG_0;
  case 1:
    fragment = new MyFragmentOne();
    tag = FragmentTags.TAG_3;
  case 2:
    fragment = new MyFragmentTwo();
    tag = FragmentTags.TAG_2;

/*OPTIONAL We can pass arguments to the fragments
Bundle args = new Bundle();
args.putInt(Arguments.ARG_POSITION, mNum);

//Place the fragment in the container
ft.replace( fragment, tag);

//You need a base layout for all fragment and use nested fragments later or you can define the layout for each position(mNum) inside the switch.
return inflater.inflate(R.layout.fragment_layout_default_for_all_views, container,

Like this you will have a good architecture and once it is working like this should be fine.

Anyway you must know how the viewPager works populating the fragment in the different positions.

When you start on the position 0, then the fragment on the position 0 and the one of the position 1 are created.

Then when you swipe to the position 1 the fragment on the 2 position is created, so you have now the three fragments created on the different positions (0,1,2..assuming you have only 3 pages on the viewPager).

We swipe to the position 2, the last one, and the fragment on the first position (0) get destroy, so we have now the fragments on the positions 2 and 3.

I hope it helped and let me know if you have any problem. Cheers

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download