1

I am creating an app that has a list of products. I display one product per fragment (total is 5 so far and i display a hard code photo on the fragment) but for now i need to save a variable when the fragment is created/shown.

When i start the app fragment 1 (postion 0 in the code) is shown, then i slide to the left and fragment 2 is shown, then fragment 3, and so on. The variable in each fragment code pulls a string from an ArrayList. If were on fragment 1 then itll take the first string in the array. If were on fragment 2 then itll take the second string from the Array and so on. This means the product/fragment being shown matches with the correct string.

The problem is that when i get to fragment 3 (or at any other point) and slide to the right fragment 2 gets displayed but for some reason my code thinks i am on fragment 1 and now the strings dont represent the correct product/fragment being shown.

It is important that each product and string variable match up perfectly.

FragmentStatePagerAdapter code:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public class SwipeAdapter extends FragmentStatePagerAdapter {

public SwipeAdapter(FragmentManager fm) {
    super(fm);
}

public static String Product;

Fragment fragment = null;

@Override
public Fragment getItem(int position) {

    if (position == 0) {
        fragment = new BuyerHomePageFragment();//Calls the fragment.
    }

    if (position == 1) {
        fragment = new BuyerHomePageFragment2();//Calls the fragment.
    }

    if (position == 2) {
        fragment = new BuyerHomePageFragment3();//Calls the fragment.
    }

    if (position == 3) {
        fragment = new BuyerHomePageFragment4();//Calls the fragment.
    }

    if (position == 4) {
        fragment = new BuyerHomePageFragment5();//Calls the fragment;
    }
    return fragment;
}

@Override
public int getCount() {

    int Count;
    Count = 5;

    return Count;//Sets the specific number of pages.
}
}

Fragment code:

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

public class BuyerHomePageFragment extends Fragment {
public static String ProductOne;
public static boolean ProductOneActive = false;

public BuyerHomePageFragment() {
    // Required empty public constructor
}

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

    View view = inflater.inflate(R.layout.fragment_buyer_home_page, container, false);


    //"Product" is a string that holds the first string the the array list "Products".
    //In the different fragments the index for the ArrayList changes to 1, 2, etc.
    //Every fragment has the same code as this one. Except the numbers increment. 


    SwipeAdapter.Product = BuyerHomePage.Products.get(0);

    return view;
}
}

Android Manager logs:

//The start(fragment 0 or product 1)
04-07 23:08:00.028 3553-3553/com.wilsapp.wilsapp W/FragmentManager: moveToState: Fragment state for BuyerHomePageFragment2{a04fed4 #0 id=0x7f0d00be} not updated inline; expected state 3 found 2

//One slide to the left and the second fragment is shown. All is good
04-07 23:08:09.123 3553-3553/com.wilsapp.wilsapp W/FragmentManager: moveToState: Fragment state for BuyerHomePageFragment3{a062d70 #2 id=0x7f0d00be} not updated inline; expected state 3 found 2

//One more slide and the third fragment is shown and still all is good
//The variables match up with the product perfectly.
04-07 23:08:10.867 3553-3553/com.wilsapp.wilsapp W/FragmentManager: moveToState: Fragment state for BuyerHomePageFragment4{db48c21 #3 id=0x7f0d00be} not updated inline; expected state 3 found 2

//Now i slid to the RIGHT and the 2nd fragment is shown but
//now it restarts the position and the string that represents the first product is linked with
//the second product.
04-07 23:08:15.598 3553-3553/com.wilsapp.wilsapp W/FragmentManager: moveToState: Fragment state for BuyerHomePageFragment{1b69fa3 #1 id=0x7f0d00be} not updated inline; expected state 3 found 2

how can i go back a fragment and not restart the position so i lose the represented product string?

Anything would help!


----------***ANSWER***------------

Instead of looking through the methods and endlessly looking through forms for the bug fix or explanation i went with this:

private static boolean m_iAmVisible;

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    m_iAmVisible = isVisibleToUser;

    if (m_iAmVisible) {
        SwipeAdapter.Product = BuyerHomePage.Products.get(0);
    }
}

This is a method that i found here on this other Stack Overflow post. It basically checks to see if the fragment is visible to the user and thats where i set my variable. Now i can go how every may times forward and as may times back and the right variable gets updated!

Also you put this method in each fragment code!

Community
  • 1
  • 1
Lazar Kukolj
  • 696
  • 3
  • 15
  • 43

0 Answers0