如何从所有其他片段导航到特定片段?

时间:2015-09-16 08:58:35

标签: android android-fragments android-activity

当我从Home_Fragment按回按钮时,控件将转到空白页面,当我从Home_Fragment按回按钮时,我想退出我的应用程序,同时当我按下我想要的所有其他片段的按钮时导航到Home_Fragment。我的MainActivity页面如下所示。

  public class MainActivity extends ActionBarActivity implements FragmentDrawer.FragmentDrawerListener {

private static String TAG = MainActivity.class.getSimpleName();

private Toolbar mToolbar;
private FragmentDrawer drawerFragment;

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

    mToolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    drawerFragment = (FragmentDrawer)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
    drawerFragment.setDrawerListener(this);

    // display the first navigation drawer view on app launch
    displayView(0);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    if(id == R.id.action_search){
        Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onDrawerItemSelected(View view, int position) {
        displayView(position);
}

private void displayView(int position) {
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position) {
        case 0:
            fragment = new HomeFragment();
            title = getString(R.string.title_home);
            break;
        case 1:
            fragment = new FriendsFragment();
            title = getString(R.string.title_friends);
            break;
        case 2:
            fragment = new MessagesFragment();
            title = getString(R.string.title_messages);
            break;
        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

        // set the toolbar title
        getSupportActionBar().setTitle(title);
    }
}

}

2 个答案:

答案 0 :(得分:4)

您可以编写onBackButton的代码来检查BackStackEntry的计数。如果其中至少有一个fragement,则Count将超过0。  如果count==0则执行finish()

@Override
    public void onBackPressed() {
    FragmentManager fm = getSupportFragmentManager();
        if (fm.getBackStackEntryCount() == 1) {
           //no fragments left
            finish();
        } else {
            super.onBackPressed();
        }

    }

答案 1 :(得分:0)

你可以尝试这个,它有效:

在主Activity中创建一个方法来调用片段:

public void replaceFragment(Fragment fragmentName){
FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragmentName);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
}

通过以下代码行按任何片段调用此方法: -

((MainActivity)getActivity).replaceFragment(FragmentName.class)