Home > Back-end >  Pass data between fragments and view pager2
Pass data between fragments and view pager2

Time:04-13

enter image description here

I have a main fragment and two fragments in view pager ( About/ Reviews). I call api to load data when navigation bar selected. I want to data model I have called api can be used between three fragments. I don't want to call api three times when the fragments load

CodePudding user response:

ok you want to call the api only once , that is when the activity is created right ?

ok for that initialize a int variable and set the value to 0;

int a=0;

then use condition to call your api

if(a==0)
{
   //Your code To call Api
   a=1;
}

So Here After U call Your Api Once "a is set to 1" which does not satisfy the condition and it does not call the api for the second time ...

but "a=0" when the your class or activity is created or called ..the api is also called

This Solution Is Given , Keeping That The Activity Is Not Recalled Or Recreated Unnecessarily ( Or The Activity Is Not Recalled/Recreated On Changing The Fragment )

CodePudding user response:

when you create the fragment just pass the data.

        Fragment fragment = new DemoFragment();
        Bundle args = new Bundle();
        args.putString("TERM", "FINEL TERM");
        fragment.setArguments(args);

You can receive data from the fragment

    Bundle args = getArguments();
    String termStatus = args.getString("TERM")
  • Related