S. Kortekaas
S. Kortekaas

Reputation: 23

Android Fragment in the wrong place

I have a weird problem.

In the main activity I use this code:

    public class MyPagerAdapter extends FragmentPagerAdapter {

    private final String[] TITLES = {"Laatste Nieuws", "Uitrukken", "Voertuigen", "Contact", "Top Grossing"};

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

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public Fragment getItem(int position) {

        Fragment fragment = null;

        switch (position) {
            case 0:
                fragment = NaviContentFragment.newInstance(position);
                break;
            case 1:
                fragment = NaviContentFragmentTwo.newInstance(position);
                break;
            case 2:
                fragment = NaviContentFragment.newInstance(position);
                break;
            case 3:
                fragment = NaviContentFragment.newInstance(position);
                break;
            case 4:
                fragment = NaviContentFragment.newInstance(position);
                break;
            default:
                break;
        }

        return fragment;
    }
}

So when I open a tab it will open a new fragment. And when I open tab 1 labeled "Uitrukken" it should open a other fragment than the other ones. This fragment:

public class NaviContentFragmentTwo extends Fragment {

private static final String ARG_POSITION = "position";

public static NaviContentFragment newInstance(int position) {

    NaviContentFragment fragment = new NaviContentFragment();
    Bundle b = new Bundle();
    b.putInt(ARG_POSITION, position);
    fragment.setArguments(b);

    return fragment;
}


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

    View rootView = rootView = inflater.inflate(R.layout.navi_content_two, container, false);

    TextView textViewNaviConTwo = (TextView) rootView.findViewById(R.id.textViewNaviConTwo);

    textViewNaviConTwo.setText("HALLLOOOTJES :)");

    return rootView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}
}

With the Text "HALLLOOOTJES :)". And only in that fragment. But the other way around happens. This text is show on all the tabs except for tab number 1.

I'm certainly missing something.

Screenshots:

It should say "HALLLOOOTJES :)"

Upvotes: 0

Views: 187

Answers (2)

fmpsagara
fmpsagara

Reputation: 485

This code could be the possible cause:

public static NaviContentFragment newInstance(int position) {

NaviContentFragment fragment = new NaviContentFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
fragment.setArguments(b);

return fragment;
}

Try replacing NaviContentFragment to NaviContentFragmentTwo.

Upvotes: 0

IAmGroot
IAmGroot

Reputation: 13855

In your NaviContentFragmentTwo class, you are returning a NaviContentFragment from your newInstance method.

My guess is you should be returning a NaviContentFragmentTwo instance.

So it should be something like..

public class NaviContentFragmentTwo extends Fragment {

  private static final String ARG_POSITION = "position";
  public static NaviContentFragmentTwo newInstance(int position) {
       NaviContentFragmentTwo fragment = new NaviContentFragmentTwo ();
       Bundle b = new Bundle();
       b.putInt(ARG_POSITION, position);
       fragment.setArguments(b);

       return fragment;
  }

Upvotes: 1

Related Questions