How to handle reloading fragments in Android?

I created an Activity with a custom footer that has 4 buttons.

Code for onClick footers:

 @Override
public void onClick(View v)
{
    try
    {
        switch (v.getId())
        {
            case R.id.button1:

                    getSupportFragmentManager()
                            .beginTransaction()
                            .addToBackStack(null)
                            .replace(R.id.flcontent,Tab1)
                            .commit();

                break;

            case R.id.button2:

                getSupportFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.flcontent,Tab2)
                        .commit();

                break;

            case R.id.button3:

                getSupportFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.flcontent, Tab3)
                        .commit();

                break;

            case R.id.button4:


                getSupportFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.flcontent, Tab4)
                        .commit();

                break;



        }
    } catch (Exception e)
    {
        e.printStackTrace();
    }

}

In the onCreate method for tabs, I make a call to the server to retrieve the list. Different tabs have different requests.

When I press button 2, Tab2 loads and so on to Button4, Tab4

For example, when I move from Tab1 to Tab2: When I press Button2, Tab1 is replaced by Tab2, and a new list is loaded. When I press Button1 back, how can I prevent the server from being called again in Tab1?

+4
source share
2 answers

, , , .

, . tabl tab2

//Add the below method to your tab1 fragment
@Override
void onDetach (){
   // write code here that would tell the server to stop loading data
   // you can also use onPause and onStop
}

Android https://developer.android.com/guide/components/fragments.html#Creating

0

- Singleton, , .

let, Tab1, , - Singleton. -, .

, (, - ..) , .

.

@ KhizarHayat , , , .

0

All Articles