Search code examples
javaandroidandroid-fragmentsfragmentandroid-viewpager2

Pass data between fragments and view pager2


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.


Solution

  • 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 )