0

I want to invoke a (tabbed)fragment method to bring the data to activity.

  1. I read this
    • But I get null object reference
  2. I tried this
    • But my activity is already implementing the fragment class so it's not a legal expression

So when the tab is changed I want the form of the first tab to send to details to Activity by getDetails() method

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            if (tab.getText().equals("Owner")) {
                //invoking method here
                Log.i(TAG, tab.getText().toString() + " unselected");
            }

I tried with FragmentManager but no luck, Please help!

EDIT- This is the method I want to invoke

public Bundle getData() {
    Bundle bundle = new Bundle();
    bundle.putString("area", area);
    bundle.putString("villageTown", villageTown.getSelectedItem().toString());
    bundle.putString("ownerName", ownerName);
    bundle.putString("fatherHusbandName", fatherHusbandName);
    bundle.putString("category", category.getSelectedItem().toString());
    bundle.putString("address", address);
    bundle.putString("mobileNumber", mobileNumber);
    bundle.putString("telephoneNumber", telephoneNumber);


    return bundle;
}
Ashvin Sharma
  • 563
  • 1
  • 5
  • 24

3 Answers3

1

I assume you're using a ViewPager binded to a Tab. First you need to keep instances of your fragment which you use in your ViewPager.

I explained how you can get get the fragment instances which you use in ViewPager in this answer. https://stackoverflow.com/a/39274141/1559852

You need a different onPageChangeListener implementation for your issue. You need to keep lastSelectedPosition of ViewPager and after page selected send your data to activity and than update the lastSelectedPosition. Here's an example implementation

    int lastSelectedPosition = viewPager.getCurrentItem();

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            // Here's your instance
            YourFragment fragment =(YourFragment)yourPagerAdapter.getRegisteredFragment(lastSelectedPosition);
            // Here're your details. You can update.
            YourDetails details = yourFragment.getDetils();
            lastSelectedPosition = position;
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

I hope this'll help you. Good luck.

savepopulation
  • 11,736
  • 4
  • 55
  • 80
1
@Override
public void onTabUnselected(TabLayout.Tab tab) {
    if (tab.getText().toString().equalsIgnoreCase("owner")) {
        OwnerForm frag = (OwnerForm) getSupportFragmentManager()
            .getFragments()
            .get(0);
        bundle = frag.getData();
    }

    Log.i(TAG, tab.getText().toString() + " says hi"); //test statement  
}

Using getFragments().get() did the trick for me, I didn't know FragmentManager keeps a list of all the active Fragments so you don't have to use getFragmentById() which causes the null object error.

Ashvin Sharma
  • 563
  • 1
  • 5
  • 24
0

I would highly suggest keeping the fragments logic in fragment. Fragment invoking a class from activity can be done with many ways, like interfaces. But, Activity calling a method of fragments will cause problems.When you especially try to reach a fragment's method which is not shown on screen.

Still, you can write your data to shared preferences from fragment when form is filled, and when onTabUnselected called you can read data from shared preferences in your activity. I accept that it is not a nice solution. I hope I understood your problem correctly.

Orcun
  • 650
  • 1
  • 7
  • 16