Get Value of different field in fragment from the parent Activity

前端 未结 4 747
借酒劲吻你
借酒劲吻你 2021-01-25 06:22

I\'ve an addActivity hosting 5 fragments, on each fragments I\'ve some fields to fill.
I want to get the value of those fields from the addActivity

4条回答
  •  抹茶落季
    2021-01-25 07:07

    Hello Chlebta You need to set pager.setOffscreenPageLimit(5);. When you swipe between fragments the fragment can get destroy. that is why you getting null.

    Alternatively an easy solution would be that you set

    pager.setOnPageChangeListener(new OnPageChangeListener() {
    
                    @Override
                    public void onPageSelected(int arg0) {
                        // Now here you get your fragment object. 
                       // To get the Fragment Object you need to store them in an ArrayList in your FragPagerAdapter class.
                      //  create a method `getMyFraggment ` which return your Fragment from Arraylist.
                     //   Now create a method in your each Fragment which return your EditText values. Here you can use HashMap to store the values.
    
                    // Here you use switch cases to get your Fragment Object I just wrote 1 here                        
                    MyFragment f =  ((FragPagerAdapter)pager.getAdapter()).getMyFraggment(arg0);
    
                    HashMap  hashMap = f.getValuesFromFragment();
                    myValues.putAll(hashMap);    // myValues is global hashmap in your Activity. 
                   // Now you get All values in your Global HashMap. But remember here you can get duplicate values if user swipe a fragment more than 1 time. You should do a logic here to remove duplicate values.
    
                    }
    
                    @Override
                    public void onPageScrolled(int arg0, float arg1, int arg2) {
                        // TODO Auto-generated method stub
    
                    }
    
                    @Override
                    public void onPageScrollStateChanged(int arg0) {
                        // TODO Auto-generated method stub
    
                    }
                });
    

提交回复
热议问题