2

When I am pressing back button getting the fragment as empty. Not getting the previous fragment poped back. Tried many solutions but didn't work. What is the solution for this ? I have no idea how to solve this.

private void displayView(int position) {
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = mHomeFragmentInstance;
        break;
    case 1:
        fragment = mOrderFragmentInstance;
        break;
    case 2:
        fragment = mOrdersheetFragmentInstance;
        break;
    case 3:
        fragment = mMyorderwidgetFragmentInstance;
        break;
    case 4:
        Intent intent=new Intent(this,LoginActivity.class);
        startActivity(intent);
        clearValues();
        finish();
        return;
    default:
        fragment = mHomeFragmentInstance;
        break;
    }

    if (fragment != null) {
        replaceFragment(fragment, position);
    }

}


/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

public void replaceFragment(Fragment fragment){     
    if (fragment != null) {
        Fragment currentFragment = this.getSupportFragmentManager().findFragmentById(R.id.frame_container);
        if(null != currentFragment ){
            if(currentFragment.equals(fragment)){
                return;
            }else{
                getSupportFragmentManager().beginTransaction().remove(currentFragment).commit();
            }
        }

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.frame_container, fragment);
        ft.addToBackStack(null);
        ft.commit();

    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

public void replaceFragment(Fragment fragment, int position){
    adapter.selectItem(position);
    Log.i("TAG", "replaceFragment:"+fragment.getClass().getName()+":"+position);
    replaceFragment(fragment);
    Log.i("TAG", "Add"+fragment.getClass().getName()+"backStackFragment");

    if(fragment.equals(mHomeFragmentInstance)){
        Log.i("TAG", "clear backStackFragment");
        backStackFragment.clear();
        backStackFragment.add(mHomeFragmentInstance);
    }

    mDrawerList.setItemChecked(position, true);
    mDrawerList.setSelection(position);
    setTitle(navMenuTitles[position]);
    mDrawerLayout.closeDrawer(drawerContainer);
}


@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    //getActionBar().setTitle(Html.fromHtml("<font color='#983A1B'>" + title + "</font>"));
    View view = getActionBar().getCustomView();
    //((TextView)view.findViewById(R.id.title)).setText(Html.fromHtml("<font color='#983A1B'>" + title + "</font>"));
    ((TextView)view.findViewById(R.id.title)).setText(Html.fromHtml("<font color='"+ ApplicationInfo.getPlfColor(this)+"'>" + title + "</font>"));
}

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

}
Nisarg Raval
  • 160
  • 12

0 Answers0