0

Sometimes getActivity() returns null, when it should not. I think it's after app not being used for some time and activity gets killed maybe. Am I wrong with adding fragments?

Also small interesting thing:

fragmentWatchList = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewpager + ":" + 0);

That string of code never find a fragment. Does anybody know why? (R.id.viewpager is correct, also tried with viewPager.getId() )

I'm using that way to add fragments:

protected void setupViewPager(ViewPager viewPager) {
    List<Fragment> fragments = getSupportFragmentManager().getFragments();
    if (fragments != null && fragments.size() == 3) {
        fragmentWatchList = fragments.get(0);
        fragmentSignalsList = fragments.get(1);
        fragmentCoinList = fragments.get(2);
    }

   // fragmentWatchList = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewpager + ":" + 0);
    if (fragmentWatchList == null)
        fragmentWatchList = new WatchListTabFragment();

    //fragmentSignalsList = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewpager + ":" + 1);
    if (fragmentSignalsList == null)
        fragmentSignalsList = new SignalsTabFragment();

   // fragmentCoinList = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewpager + ":" + 2);
    if (fragmentCoinList == null)
        fragmentCoinList = new CoinsTabFragment();

    viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

    viewPagerAdapter.addFragment(fragmentWatchList);
    viewPagerAdapter.addFragment(fragmentSignalsList);
    viewPagerAdapter.addFragment(fragmentCoinList);

    viewPager.setAdapter(viewPagerAdapter);
}
ZIA ANSARI
  • 131
  • 1
  • 11
Vadim
  • 335
  • 3
  • 14

2 Answers2

0

The findFragmentByTag works when you setTag to the fragment your calling via the FragmentManager. Something like this:

Fragment f = new Fragment();

getSupportFragmentManager().beginTransaction().replace(id,f, "tag_comes_here").commit();

Also, the getActivity() returning null is mostly because the fragment was removed from the holding activity. Hence its good to make a isAdded() check for async calls inside a fragment.

Check this SO post here which describes neatly the use of isAdded() and why is it needed

Happy coding~

MadScientist
  • 2,134
  • 14
  • 27
  • There should be a default tag (which i am looking for with commended strings) . I know about isAdded(), but app is useless, if fragment is not added - is there a way to attach to new MainActivity may be or to avoid that situation at all? – Vadim Nov 22 '17 at 12:13
0

The correct way to implement a regular FragmentPagerAdapter is:

public class ViewPagerAdapter extends FragmentPagerAdapter {
    public ViewPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public Fragment getItem(int position) {
        if(position == 0) return new RulesFragment();
        if(position == 1) return new TreeFragment();
        if(position == 2) return new PredictionFragment();

        throw new IllegalStateException("Unexpected position " + position);
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        if(position == 0) return "TREE RULES";
        if(position == 1) return "REGRESSION TREE";
        if(position == 2) return "PREDICTION";
      
        throw new IllegalStateException("Unexpected position " + position);
    }
}

To get a reference to a Fragment created by a ViewPager, use the following findFragmentByTag scheme:

Fragment fragment = supportFragmentManager.findFragmentByTag("android:switcher:" + viewPager.getId() + ":" + fragmentPosition)
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428