Mehran Mehran - 4 years ago 130
Android Question

Fragment returns view to other fragments

I have four fragments(tab swipe stuff) ... for example i created a toast in my fragment #2 , but this toast also comes up in my fragment #4 when i open it ..

this is my fragment #2

public class A extends Fragment {
View view ;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.xxx_updates_layout, container, false);

//Custom Toast
LayoutInflater inflaterToast = getActivity().getLayoutInflater();
View layout = inflaterToast.inflate(R.layout.d_toast_d,
(ViewGroup) view.findViewById(R.id.toast_layout_root));

ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_people);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Ringtone Bank");
Toast toast = new Toast(getActivity().getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();


return view;
} //onCreate
}


This is my fragment #4

public class XXXSentFragment extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.xxx_sent_layout,null);
}
}


My Fragment Pager Adapter

public class XXXTabFragment extends Fragment {

public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 4 ;

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

View x = inflater.inflate(R.layout.xxx_tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);


viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));



tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});

return x;

}

class MyAdapter extends FragmentPagerAdapter{

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



@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new XXXPrimaryFragment();
case 1 : return new XXXSocialFragment();
case 2 : return new XXXUpdatesFragment();
case 3 : return new XXXFOUR();
}
return null;
}

@Override
public int getCount() {

return int_items;

}



@Override
public CharSequence getPageTitle(int position) {

switch (position){
case 0 :
return "Primary";
case 1 :
return "Social";
case 2 :
return "Updates";
case 3 :
return "Four";
}
return null;
}
}

}

Answer Source

The thing is that View Page loads 3 fragments at a time (the one being shown, the one on the left and the one on the right). Hence the onCreateView for all of them get called at once which is why the toast from another fragment appears.Put a log statement in the onCreateView of all your fragments to verify this and to get a better understanding of how this works.

If your use case is to show a toast when the user swipes or selects a certain fragment (In your case XXXUpdatesFragment()), one way to go about this is to hold references to your fragments in your FragmentPagerAdapter and then implement the onPageChangeListener on the viewPager, when the page changes to 2, use the reference to call a method inside XXXUpdatesFragment() which will show a toast.

public class XXXUpdatesFragment extends Fragment {
    View view ;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.xxx_updates_layout, container, false);


    return view;
   } 

  public void showToast() {
    //Custom Toast
    LayoutInflater inflaterToast = getActivity().getLayoutInflater();
    View layout = inflaterToast.inflate(R.layout.d_toast_d,
             (ViewGroup) view.findViewById(R.id.toast_layout_root));

    ImageView image = (ImageView) layout.findViewById(R.id.image);
    image.setImageResource(R.drawable.ic_people);
    TextView text = (TextView) layout.findViewById(R.id.text);
    text.setText("Ringtone Bank");
    Toast toast = new Toast(getActivity().getApplicationContext());
    toast.setGravity(Gravity.BOTTOM, 0, 0);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(layout);
    toast.show();
  }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download