0

I have tried all solution on stackoverflow but none help me.

When I back from Default fragment I want to show fragment 0 but here application is close

Below is MainActivity code

   private void selectItem(int position) {

    switch (position) {
    case 0:
        getSupportFragmentManager()
                .beginTransaction().addToBackStack(PageSlidingTabStripFragment.TAG)
                .replace(R.id.content,
                        PageSlidingTabStripFragment.newInstance(),
                        PageSlidingTabStripFragment.TAG).commit();
        break;
    default:

        SherlockFragment fragment = new PlanetFragment();
        Bundle args = new Bundle();
        args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
        fragment.setArguments(args);

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content, fragment).commit();
        break;
    }

    mDrawerLayout.closeDrawer(mDrawerList);
}

Second Fragment back activity

public void onBackPressed() {
        FragmentManager fm = getFragmentManager();
        if(fm.getBackStackEntryCount() != 0) {
            fm.popBackStack();
            fm.getBackStackEntryAt(0);

        } else {
            getActivity().onBackPressed();
        }
    }
Community
  • 1
  • 1
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
  • Check how an activity communicates with a fragment using an interface. – Skynet Apr 08 '16 at 13:49
  • Possible duplicate of [How can I control the activity's up button from a contained fragment?](http://stackoverflow.com/questions/34025331/how-can-i-control-the-activitys-up-button-from-a-contained-fragment) – Daniel Nugent Apr 08 '16 at 15:42

1 Answers1

0

Bro change your conditions in onBackPressed() : Like

Integer backStackSize = getFragmentManager().getBackStackEntryCount();

        if(backStackSize < 2){
            //new Logout_Dialog(MainActivity.this, MainActivity.this);
//YOU CAN ASK USER WHETHER HE WANTS TO LOGOUT OR NOT HERE......
        }
        else if(backStackSize >= 2){

            FragmentManager.BackStackEntry backEntry=getFragmentManager().getBackStackEntryAt(getFragmentManager().getBackStackEntryCount()-1);


                fragmentManager.popBackStack();
        }//else if(backStackSize >= 2) closes here.....
        else
        {
            //Else fragment shud be removed from Backstack.... 
            super.onBackPressed();
        }//fragment shud be removed from Backstack..
Pawan
  • 533
  • 1
  • 7
  • 31
  • got The method onBackPressed() is undefined for the type SherlockFragment – Nirav Ranpara Apr 08 '16 at 14:01
  • @Nirav Ranpara : No, do this in your BaseActivity where you are inflating the Fragment. Fragments does not have onBackPressed(), Activities have. – Pawan Apr 08 '16 at 14:02
  • @NiravRanpara : In your MainActivity. (Assuming your MainActivity extends Activity) If some other class which extends Activity & inflates your fragment then write the code there. – Pawan Apr 08 '16 at 14:05
  • done ! no action happen . display same fragment after pressing evern 10 times back – Nirav Ranpara Apr 08 '16 at 14:06
  • @NiravRanpara : your control must be gng in `if(backStackSize < 2){ //new Logout_Dialog(MainActivity.this, MainActivity.this); }` Put some Log here & check. P.s. Png home now, will reply you after 2 hrs. – Pawan Apr 08 '16 at 14:10
  • @NiravRanpara : Is your issue fixed ? – Pawan Apr 08 '16 at 15:35
  • Did you tried to put the log ? Is the control coming in if condition ? Try debugging your code & just chk the control is coming till what line. – Pawan Apr 09 '16 at 10:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108694/discussion-between-pawan-and-nirav-ranpara). – Pawan Apr 09 '16 at 10:16