如何从android中的片段返回页面或返回片段?

时间:2015-11-23 07:24:16

标签: android navigation-drawer onbackpressed

我正在使用包含许多片段的抽屉布局,当我从特定片段按下按钮时,它直接从应用程序中移出。我看到很多像onBackpress()方法的建议。以下是我的代码..我无法理解在何处实现此代码和howto。有人会帮助我吗?

private void selectItemFragment(int position){

    Fragment fragment = null;
    fragmentManager = getSupportFragmentManager();
    switch(position) {
        default:
        case 0:
            fragment = new ProfileFragment();

            // Toast.makeText(getApplicationContext(),"Profile Fragment 1"+position,Toast.LENGTH_LONG).show();
            break;
        case 1:
            fragment = new UploadQuestionFragment();
            //Toast.makeText(getApplicationContext(),"Fragment 2 "+position,Toast.LENGTH_LONG).show();
            break;
        case 2:
            fragment = new DefaultFragment();
            //Toast.makeText(getApplicationContext(),"Fragment 3"+position,Toast.LENGTH_LONG).show();
            break;
        case 3:
            fragment = new GetAllAnswerFragment();
            //Toast.makeText(getApplicationContext(),"All Answer Fragment 4 "+position,Toast.LENGTH_LONG).show();
            break;
        case 4:
            fragment = new AllQuestionFragment();
            //Toast.makeText(getApplicationContext(),"Fragment 5 "+position,Toast.LENGTH_LONG).show();
            break;
        case 5:
            fragment = null;
            break;
    }

    if(fragment !=null){
        fragmentManager.beginTransaction().replace(R.id.main_fragment_container, fragment).commit();

        mDrawerList.setItemChecked(position, true);
        setTitle(titles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }else{
        Intent intent = new Intent(MainActivity.this, FirstActivity.class);
        startActivity(intent);
        finish();
    }

}

2 个答案:

答案 0 :(得分:0)

你需要添加到backstack并从backstack弹出才能实现它

要添加到Backstack,请使用

public void showFragment(Fragment fragment, String title) {
    if (fragment != null) {
        String backStateName = fragment.getClass().getName();
        mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.replace(R.id.container_frame, fragment);
        mFragmentTransaction.addToBackStack(backStateName);
        mFragmentTransaction.commit();
        setActionBarTitle(title);
        if (backStateName == DashBoardFragment.class.getSimpleName()) {
            clearBackStack();
        }
    }
}

从后台使用

弹出
private void popFragments(int backStackCount) {
    String fragmentTag = mFragmentManager.getBackStackEntryAt(
            backStackCount - 1).getName();
    if (!TextUtils.isEmpty(fragmentTag)) {
        Fragment currentFragment = mFragmentManager
                .findFragmentById(R.id.container_frame);
        if (currentFragment != null) {
            finish();
        } else {
            mFragmentManager.popBackStack();
        }
    }
}

答案 1 :(得分:0)

@opu ...

fragmentManager.beginTransaction().replace(R.id.main_fragment_container, fragment).commit();

使用add()方法代替replace()方法

  

注意 - 对于片段的第一次调用,否则使用replace()方法   使用add()方法

add()方法,也是addBackStack()方法将片段堆叠到另一个

fragment stack using backstack

因此使用

//for first fragment
getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.main_activity_content_frame, new RestaurantsListFragment(), "fragment" + i)
            //.addToBackStack("fragment" + (i++))
            .commit();

// for other fragment
getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.main_activity_content_frame, new RestaurantsListFragment(), "fragment" + i)
            .addToBackStack("fragment" + (i++))
            .commit();


// on back press
 @Override
public void onBackPressed() {
    int count = getSupportFragmentManager().getBackStackEntryCount();
    Log.e("POP FRAGMENT STACK", "Count ="+count);
    if(count==0){
        showExitDialog();
    }else{
        Log.e("POP FRAGMENT STACK", "Fragment Info =" + getSupportFragmentManager().getFragments().get(count - 1).getTag());
        getSupportFragmentManager().popBackStack();
    }
}