以编程方式返回上一个片段

时间:2016-08-16 09:30:48

标签: android android-fragments

如何让onBackPressed()从每个片段进入HomeFragment或转到之前的片段。

以下是默认情况下设置片段的代码主页片段在应用启动时打开但在后台按下应用关闭。 需要帮助解决它谢谢 这是我的MainActivityDrawer类

private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
GpsTrackerAlarmReceiver alarm = new GpsTrackerAlarmReceiver();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_drawer);

    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);
    alarm.setAlarm(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) {
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_help) {
        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 ProfileFragment();
            title = getString(R.string.title_profile);
            break;
        case 2:

            fragment = new ContactsFragment();
            title = getString(R.string.title_contacts);
            break;
        case 3:
            fragment = new GeoFencFragment();
            title = getString(R.string.title_geofence);
            break;
        case 4:
            fragment = new HistoryFragment();
            title = getString(R.string.title_history);
            break;
        case 5:
            fragment = new SettingFragment();
            title = getString(R.string.title_settimg);
            break;
        default:
            break;
    }
    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.commit();
        // set the toolbar title
        getSupportActionBar().setTitle(title);
    }
}
@Override
public void onResume() {
    Log.d(TAG, "onResume");
    super.onResume();
}
@Override
protected void onStop() {
    super.onStop();
}}

2 个答案:

答案 0 :(得分:1)

我是通过以下方式实现的: - 1.不在后台堆栈中添加片段事务。 2.在背面按下检查当前显示的片段是否不是主片段,然后显示其他明智的super.onBackPressd()的家庭片段以关闭活动。

 if (mDrawerLayout.isDrawerOpen(GravityCompat.START))
        {
            mDrawerLayout.closeDrawer(GravityCompat.START);
        }
     else if (fragmentManager.findFragmentById(R.id.content_frame_drawer_layout) != homeFragment)
                {
                    fragmentManager.beginTransaction().replace(R.id.content_frame_drawer_layout, homeFragment).commit();

                }
                else
                {
                    super.onBackPressed();
                }

答案 1 :(得分:0)

试试这个

getFragmentManager().beginTransaction().
    remove(this).commit();

或使用popBackStackImmediate()

在活动

中的commit()之前使用addToBackstack()

如果要删除片段,请使用popBackStackImmediate()