0

I have Two Fragments one will Send integers to the Sql database, and the other will display them both fragments are working fine. However, when I send an integer to the database and I swipe to the display fragment it will not refresh until push a button, I would like to make it automatically once I move to the fragment please: Here is my Main Activity:

public class MainActivity extends AppCompatActivity   {
 int index;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(activity_main);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


    ViewPager mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.getAdapter().notifyDataSetChanged();

    mViewPager.setOffscreenPageLimit(0);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);


    //------------- Intlize Fragment ----//
    Dash dash = new Dash();
    getSupportFragmentManager().beginTransaction().add(R.id.dashfragment, dash).commit();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

        if (id == R.id.action_settings) {
        Intent i = new Intent(this, AboutMe.class );
        startActivity(i);
    }

    return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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



    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                Dash dash = new Dash();
                return dash;
            case 1:
                Caluclate caluclate = new Caluclate();
                return caluclate;
            case 2:
                AskMe askMe = new AskMe();
                return askMe;
            case 3:
                TeamPicker teamPicker = new TeamPicker();
                return teamPicker;
        }
        return null;
    }

i read many solutions but i couldn't figure it out.

Ahmad Meer
  • 89
  • 1
  • 9

1 Answers1

0

You could do something like

if (fragment.isVisible()) {
   refreshItFirst;
}

BTW why take a fragment for insertions of Db - there is a helper class?

The method isVisible() shoud help you.

Look at this post Replace Fragment inside a ViewPager

Tobias Alt
  • 453
  • 1
  • 3
  • 11
  • where do i implement this please, i have no idea. also doesn't the helper class lose its data once the application is terminated. – Ahmad Meer Jul 01 '17 at 12:52
  • The helper class does not store persistently data - that does your Db. But my idea was to implement your method/action when you "push a button" as you called it in the display fragment if it is visible -> do refresh (push button action). – Tobias Alt Jul 01 '17 at 13:07
  • Another aspect could be - that you create everytime a new fragment instance - maybe replace your old one with the new one is an idea! – Tobias Alt Jul 01 '17 at 13:18
  • i see where your going with this, but i prefer to just destroy the fragment once its not visible or something. – Ahmad Meer Jul 01 '17 at 13:28