title: Start activity in a Fragment
body: <p>I've decided to add more tablet-friendly UI to my app by creating a dual-pane layout using the new fragments API. But the problem is that lots of screens in my app are Activity subclasses. Manually converting them all to fragments is not an option because:<br/>
  - There are nearly 50 activities.<br/>
  - I'd like my app to be compatible with all versions of Android starting at 1.6<br/>
  - And I'd like it to be as small as possible so using a compatibility library is not an option as it is too huge<br/></p>
  
  <p>Although I've found <a href="https://stackoverflow.com/questions/6249031/start-activity-in-a-fragment">some</a> <a href="https://stackoverflow.com/questions/8510165/loading-activity-in-fragment-code">questions</a> whose answers are saying that it is impossible, I've done it almost successfully. Here is code of my custom Fragment:</p>
  
  <pre><code>public static class ActivityFragment extends Fragment{
      Intent intent;
      View view;
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          if(intent==null){
              Bundle a=getArguments();
              intent=a.getParcelable("intent");
          }
          LocalActivityManager am=((ActivityGroup)getActivity()).getLocalActivityManager();
          Window wnd=am.startActivity("intent"+intent.hashCode(), intent);
          if(view==null){
              view=wnd.getDecorView();
              view.setLayoutParams(new FrameLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
          }
          return view;
      }
  
      public void onDestroy(){
          super.onDestroy();
          if(!((TabletMainActivity)getActivity()).tabs.containsValue(this)){
              ((ActivityGroup)getActivity()).getLocalActivityManager().destroyActivity("intent"+intent.hashCode(), true);
          }
      }
  }
  </code></pre>
  
  <p>In order to work it must be used only in ActivityGroup.
  Only problem is that in some activities with a ListView method onItemClick() does not get called after the activity is resumed, i.e. I click an item, another activity starts on top of current, but when I go back, items are no longer clickable.</p>