title: Is there a viewDidAppear for Tabhost with fragments? Want to run code when user comes back to the tab
body: <p>Is there a way to run code when you go to a tab? For example you go to the second tab (runs code) and then leave it. You go back to the second tab (runs code). I'm porting my iOS app to android and in Xcode you'd just put the code in viewdidappear, is there something similar in Android?</p>
  
  <p>How my fragment looks like:</p>
  
  <pre><code>import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.v4.app.Fragment;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  public class ReportFragment extends Fragment {
  
  private View mRootView;
  
  // create boolean for fetching data
  private boolean isViewShownReport = false;
  
  @Override
  public void setUserVisibleHint(boolean isVisibleToUser) {
      super.setUserVisibleHint(isVisibleToUser);
      if (getView() != null) {
          isViewShownReport = true;
          // fetchdata() contains logic to show data when page is selected mostly asynctask to fill the data
          //fetchData();
      } else {
          isViewShownReport = false;
      }
  }
  
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      if (mRootView == null){
          Log.e("666","ReportFragment");
          mRootView = inflater.inflate(R.layout.report_fragment,container,false);
      }
      ViewGroup parent = (ViewGroup) mRootView.getParent();
      if (parent != null){
          parent.removeView(mRootView);
      }
  
      if (!isViewShownReport) {
          //fetchData();// or anything that you want to execute after tab selection
  
          //System.out.println("Hello!")
          Log.d("myTag", "ReportFragment");
      }
  
      return mRootView;
  }
  }
  </code></pre>