片段交易在哪里放?

时间:2014-01-11 06:44:38

标签: android android-fragments fragmenttransaction

我想在应用程序启动后显示homefragment,意味着在menuitem点击之前,我应该在哪里放置片段事务?谢谢!

    public boolean onOptionsItemSelected(MenuItem item) {
     // The action bar home/up action should open or close the drawer.
     // ActionBarDrawerToggle will take care of this. 

    switch(item.getItemId()){
    case android.R.id.home:
        if(mDrawerLayout.isDrawerOpen(leftDrawerLayout)){
            mDrawerLayout.closeDrawer(leftDrawerLayout);
        } else {
            mDrawerLayout.openDrawer(leftDrawerLayout);
        }
    case R.id.home:
        Toast.makeText(getApplicationContext(), "home", Toast.LENGTH_LONG).show();
        **ft.replace(R.id.content_frame, homefragment);**
        break;

    case R.id.feed:
        Toast.makeText(getApplicationContext(), "feed", Toast.LENGTH_LONG).show();
        ft.replace(R.id.content_frame, feedfragment);
        break;
    }
    ft.commit();
    return super.onOptionsItemSelected(item);

}

1 个答案:

答案 0 :(得分:0)

试试这个..

case R.id.home:
    Toast.makeText(getApplicationContext(), "home", Toast.LENGTH_LONG).show();

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ClassName homefragment = new ClassName();
    ft.replace(R.id.content_frame, homefragment);
    ft.commit();
    break;

case R.id.feed:
    Toast.makeText(getApplicationContext(), "feed", Toast.LENGTH_LONG).show();

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ClassName feedfragment = new ClassName();
    ft.replace(R.id.content_frame, feedfragment);
    ft.commit();
    break;