title: Android: fragment restore after screen orientation change
body: <p>I'm starting with fragments and I'm facing a problem. I wan't to restore my fragment after a screen rotation.
  My app looks like this: on landscape I have a button on the left area, which updates the a label on the right area. If on portrait mode, I'm navigating to a new activity. However, I wan't to maintain the fragment state after rotating.
  Code looks like this:</p>
  
  <p>Left area fragment:</p>
  
  <pre><code>public class ListFragment extends Fragment implements OnClickListener {
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
  
      View view = inflater.inflate(R.layout.fragment_list, container, false);
      Button button = (Button) view.findViewById(R.id.button1);
      button.setOnClickListener(this);
  
      return view;
  }
  
  @Override
  public void onClick(View v) {
  
      switch (v.getId()) {
  
      case R.id.button1:
          updateDetail();
          break;
  
      }
  }
  
  public void updateDetail() {
      String newTime = String.valueOf(System.currentTimeMillis());
      DetailFragment fragment = (DetailFragment) getFragmentManager()
              .findFragmentById(R.id.detailFragment);
      if (fragment != null &amp;&amp; fragment.isInLayout()) {
          fragment.setText(newTime);
      } else {
          Intent intent = new Intent(getActivity().getApplicationContext(),
                  DetailActivity.class);
          intent.putExtra("value", newTime);
          startActivity(intent);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Right area activity:</p>
  
  <pre><code>public class DetailActivity extends Activity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
          finish();
          return;
      }       
  
      if (savedInstanceState == null) {
          setContentView(R.layout.activity_detail);
          Bundle extras = getIntent().getExtras();
          if (extras != null) {
              String s = extras.getString("value");
              TextView view = (TextView) findViewById(R.id.detailsText);
              view.setText(s);
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Right area fragment:</p>
  
  <pre><code>public class DetailFragment extends Fragment {
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setRetainInstance(true);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
  
      View view = inflater
              .inflate(R.layout.fragment_detail, container, false);
  
      return view;
  }
  
  public void setText(String item) {
      TextView view = (TextView) getView().findViewById(R.id.detailsText);
      view.setText(item);
  }
  
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
  
      FragmentManager fm = getFragmentManager();
  
      DetailFragment fragment = (DetailFragment)fm.findFragmentById(R.id.detailFragment);
  
      if (fragment == null) {
          fragment = new DetailFragment();
          fragment.setTargetFragment(this, 0);
          fm.beginTransaction().add(R.id.detailFragment, fragment).commit();
      } 
  }
  </code></pre>
  
  <p>}</p>
  
  <p>What can I possibly be doing wrong?</p>