Code example for Fragment

Methods: getActivity, onActivityCreated, setRetainInstance

0
    return(result);
  } 
 
  @Override 
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
 
    setRetainInstance(true);
    getActivity().getApplicationContext()
                 .bindService(new Intent(
                                         "com.commonsware.android.advservice.IScript"), 
                              this, Context.BIND_AUTO_CREATE);
  } 
 
  @Override 
  public void onDestroy() { 
    getActivity().getApplicationContext().unbindService(this);
 
    super.onDestroy(); 
  }