Adding fragments to older Android apps

I want to upgrade a couple of my Android apps to use Fragments. Here is a basic situation:

I have an Activity and a ListActivity. I want to convert these using fragments using the compatibility packaging. Below is what I have in the onCreate method in the Activity. (The ListActivity has similar items so I am only using the Activity as the example ere in the conversion).

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    this.getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    setContentView(R.layout.rate);

    Item = getIntent().getExtras().getString("name");
    Category = getIntent().getExtras().getString("category");

    title = (TextView) findViewById(R.id.tvRateItem);
    ratingsBar = (RatingBar) findViewById(R.id.theRatingBar);

    title.setText(Item);

    // Display list of reviews

    new starTotalTask().execute();
    new starRatingTask().execute();

    final EditText etTweetReview = (EditText) findViewById(R.id.etTweetReview);
    Button button = (Button) findViewById(R.id.theRatingBarButton);
    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            // do stuff

            }
        }
    });

    Button BReviews = (Button) findViewById(R.id.bReviews);
    BReviews.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

        // do stuff 

        }
    });

}

So you see, I have a couple of Buttons, a couple of AsyncTasks, some getIntents that are grabbing data from other Activities, some "widget" declarations referencing to the XML.

I have the basic framework already setup with a FragmentActivity (from compatibility v4 package) with a couple of fragments it points to. In fact, I have a ViewPager, and tabs set up. The correct layouts are already setup as it should be to the new Fragment view. I just don't get how to move the data as seen above. I also understand the onCreateView is where you set the layout. Outside of that, where does everything else go? Not looking for code example as much as direction -- IF POSSIBLE.

Edit: My layout is based on this: http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/

especially interest in: - Where do the RatingsBar and TextView (and other xml references) go? - what about AsyncTask? - and how do you handle passing around data in intents?


Almost everything will be identical. The key differences are:

  • You need to extend FragmentActivity in place of Activity .

  • You need to call getSupportFragmentManager() instead of getFragmentManager() .

  • You need to call getSupportLoaderManager() instead of getLoaderManager() .

  • The compatibility library does not support a ListFragmentActivity (or whatever). You'll need to use a FragmentActivity instead and set up the ListView in your code.

  • 链接地址: http://www.djcxy.com/p/79052.html

    上一篇: 如何将片段显示为对话框?

    下一篇: 为旧版Android应用添加片段