-2

My app have a fragmentActivity with tabBar which control over the view pager and contain 3 fragments ,when i update my class User in fragmentB,i need to display him in fragmentC. my question is how to refresh fragmentC every time when i add new User.

before i wrote this question i tried all the solution from this questions:

1.Update ViewPager dynamically?

2.refresh fragment at reload

3.Update Fragment from ViewPager

4.ViewPager PagerAdapter not updating the View

5.How to update fragment content from activity (viewpager)?

here is my code

FragmentActivity:

public class MainActivityTab extends FragmentActivity  {


    public SectionsPagerAdapter mSectionsPagerAdapter;
    public ViewPager mViewPager;
    public static MainActivityTab instance = null;


    public static MainActivityTab getInstance(){
        return instance;
    }

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tab);
        getWindow().setStatusBarColor(Color.rgb(191,76,12));
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        tabLayout.getTabAt(0).setIcon(R.drawable.icon_A);
        tabLayout.getTabAt(1).setIcon(R.drawable.icon_B);
        tabLayout.getTabAt(2).setIcon(R.drawable.icon_C);
        mViewPager.setCurrentItem(1,false);
    }

FragmentStatePagerAdapter:

 public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return FragmentA.newInstance();
                case 1:
                    return FragmentA.newInstance();
                case 2:
                    return FragmentC.newInstance();
                default:
                    return null;
            }
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return null;
        }
    }

FragmentB:

public class FragmentB extends Fragment {

    EditText name;
    EditText age;
    Button btnSave;

    public static FragmentB newInstance() {
        FragmentB fragment = new FragmentB();
        return fragment;
    }
    public FragmentB() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_new2, container, false);
        name = (EditText) rootView.findViewById(R.id.name);
        age = (EditText) rootView.findViewById(R.id.age);
        btnSave = (Button) rootView.findViewById(R.id.btnSave);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserManager.getInstance().creteUser(name.getText().toString(),age.getText().toString());
            }
        });
        return rootView;
    }
}

FragmentC:

public class FragmentC extends Fragment {
    TextView nameTxt;
    TextView ageTxt;

    public static FragmentC newInstance(){
        FragmentC instance = new FragmentC();
        return instance;
    }
    public FragmentC(){

    }
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
     View rootView = inflater.inflate(R.layout.fragment_new3, container, false);
    nameTxt = (TextView) rootView.findViewById(R.id.nameTxt);
    ageTxt = (TextView) rootView.findViewById(R.id.ageTxt);
    showUser();
    return rootView;
    }
    public void showUser(){

        if(UserManager.getInstance().getUser().getName()!=null){
            nameTxt.setText(UserManager.getInstance().getUser().getName().toString());
            ageTxt.setText(UserManager.getInstance().getUser().getAge().toString());
        }
    }

}
Community
  • 1
  • 1
moshe kobi
  • 71
  • 1
  • 7

2 Answers2

0

You could create a callback like:

public interface UserChangedCallback{
  void onUserChanged();
}

and implement this callback on every view/fragment where you want to access the user:

@Override
public void onUserChanged() {
  adapter.notifyDataSetChanged();
  // or if you don't have an adapter refresh your textfields or whatever you want
}

Register the callback on the usermanager via UserManager.getInstance().registerCallback(this). Internally the usermanager have to add the callback to an internal list.

Create a private function inside your usermanager:

private void notifyCallbacks() {
  for(UserChangedCallback callback : registeredCallbacks) {
    callback.onUserChanged();
  } 
}

If you add/modify/delete a user you always have to call this function at the end. For example:

public void addUser(User user) {
  users.add(user);
  notifiCallbacks();
}

Now every view will be notified and refreshed.

Important!

Don't forget to unregister the callback from the usermanager on onDestroy() to avoid memory leaks.

beeb
  • 1,615
  • 1
  • 12
  • 24
  • i am kind of new in android you can explain to me how to register the callback – moshe kobi Jan 29 '17 at 16:00
  • Create a public function in your UserManager `public void registerCallback(UserChangedCallback callback);` and call this function in `onCreate()` of your view – beeb Jan 29 '17 at 18:54
  • `public void registerCallback(UserChangedCallback callback){registeredCallbacks.add(callback);}` ....if i need to do like this i get nullpointer – moshe kobi Jan 29 '17 at 19:27
  • Initialize `registeredCallbacks` in your private constructor of your usermanager. – beeb Jan 29 '17 at 22:37
  • i found solution mate,but thank you very much for the trying :) – moshe kobi Jan 30 '17 at 22:19
0

Found solution:

i used in the method onPageSelected,when position is 2 my fragment detach and afterwards attach the fragment. working perfectly!

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    if(position==2){
                        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction().add(R.id.fragmant_new3, FragmentC.newInstance());
                        fragmentTransaction.detach(FragmentC.newInstance());
                        fragmentTransaction.attach(FragmentC.newInstance());
                        fragmentTransaction.commit();

                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
moshe kobi
  • 71
  • 1
  • 7