Search This Blog

Wednesday, February 22, 2012

Porting Activity to Fragment

Oldman : "Now for Honeycomb and above phones or tablets, The Activity design method has been replace with Fragment. I spent many days learned how to porting the old Activity method source code to new Fragment. Hope this can be useful for the others... "

1.Replace main menu Activity with FragmentActivity (This is mean that I'm using Android "support libraries" instead of real Fragment api. That because I need to back porting this to running on the old devices also)

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }  
   
}

 Yes, there is not much things inside MainActivity class but just calling setContentView method to set view for R.layout.main which is ...

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <fragment class="com.myapp.TitlesFragment"
            android:id="@+id/titles"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</FrameLayout>
At res/layout/main.xml and ...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <fragment class="com.myapp.TitlesFragment"
        android:id="@+id/fragment1"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
    <FrameLayout android:id="@+id/details"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
 </LinearLayout>
At res/layout-land/main.xml

2.Create TitlesFragment class (just like they described here)

3.Change others Activity type to Fragment type. Here is somethings you need to change ...
   - onBackPressed() method must be remove.
   - Add onCreateView() method. Move all code in onCreate() method to onCreateView() method.
   - Change 'this.' to 'this.getActivity().'
   - To access view components, use 'View view = inflater.inflate(R.layout.find_scanner_view, container, false);'
   - Be careful about this.getActivity().finish(); because in this case it means go finish the application.
   - They said to using onSaveInstanceState() method to saving UI state but it seems not working probably what I expected it will. So I using SharedPreferences to save UI state and restoring them at onCreateView() method. 
   - Add xxxFragmentActivity inside the Fragment class...

public class BFragment extends Fragment implements OnClickListener,Runnable{
   ....

  public static class BFragmentActivity extends FragmentActivity {
     ....
  }

}
4. Connect to converted Fragment classes through FragmentManager (learn from the example)
5. To sending result between Fragment classes and TitlesFragment class, just override setTargetFragment() method and calling it to passing caller Fragment reference.

     static Fragment targetFragment;
     static int targetRequestCode;
     @Override
     public void setTargetFragment(Fragment fragment, int requestCode){
         super.setTargetFragment(fragment, requestCode);
         targetFragment=fragment;
         targetRequestCode=requestCode;
     }

OK, This is just the point of view. In details, May be you need more homework to do. For my opinion, I think to making new app with Fragment should be a better choice than trying to porting old app to it ... it was painful job ...

Cheers,

No comments:

Post a Comment