Search code examples
androidandroid-fragmentsandroid-lifecycle

How to make method don't call again after user returned to this fragment from another?


I have one method(let's call it getData) in my fragment(let's call it List) which I call in onCreateView. This method load some data from server and put it to some views. In my fragment I have one button which open another fragment(Let's call it Detail), and when I go back to List onCreateView calling again and data start load again.

I tried to put getData into onCreate... and in fact it must works, and mustn't call getData again...

But getData works with views whick initialize only in onCreateView and another methods after onCreateView and all that methods always recalling when I return to List from Detail.

How do I make don't recall getData when I return to List from Detail.

I'm so sorry for my bad English and grammar mistakes. Thanks.


Solution

  • first of All override onStop method by Ctrl+O it will look like this

    public class yourFragment extends Fragment{
    boolean isReturned = false;
    
    
    
    
    @Override
    public void onStop() {
        super.onStop();
        isReturned = true;
    }
    
    
    
    public void getData(){
       if(!isReturned){
        //fetch data
         }
    }