Skip to content Skip to sidebar Skip to footer

Fragment Back Stack From Second Fragment To First Fragment

I have viewpager tab fragment and from one tabb fragment on button click it open another fragment and another second fragment i want to add event of backpress as i am doing backpre

Solution 1:

Add your fragment to backstack and then in your onBackPressed method do something like this:

@Override 
public void onBackPressed() { 
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        getSupportFragmentManager().popBackStack(); 
    } else { 
        this.finish(); 
    }
}

For more information see this

Hope this is what you are looking for and it helps you.

Solution 2:

Try this (Instead of "replace" use "add")

fragmentTransaction = fragmentManager!!.beginTransaction()
fragmentTransaction.add(R.id.frame_container, paypal)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()

and

@Override 
public void onBackPressed() { 
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        getSupportFragmentManager().popBackStack(); 
    } else { 
        this.finish(); 
    }
}

Solution 3:

if fragment within fragment back use this code onbackpreesed method

@OverridepublicvoidonBackPressed() {
    DrawerLayoutdrawer= (DrawerLayout) findViewById(R.id.drawer_layout);
    if(getFragmentManager().getBackStackEntryCount() == 1) {
        newAlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Check out")
                .setMessage("want to do check out?")
                .setPositiveButton("Yes", newDialogInterface.OnClickListener() {
                    @OverridepublicvoidonClick(DialogInterface dialog, int which) {
                        closeApp();
                    }
                })
                .setNegativeButton("No",null)
                .show();

    }
    else {
        super.onBackPressed();
    }
}

each fragment store in a stack

 FragmentManager ff=getFragmentManager();
ff.beginTransaction().replace(R.id.main_content,new home()).addToBackStack(null).commit();

it's work in my project

Solution 4:

I used this in Activity

Step 1:

Created a global variable for boolean

privatebooleandoubleBackToExitPressedOnce=false;

Step 2:

Then in onBackPress() method of activity

i did this

@OverridepublicvoidonBackPressed() {
    if (mViewPager.getCurrentItem() > 0) {
        //if any tab selected instead of tab 1
        mDoubleBackToExitPressedOnce = false;
    } elseif (mViewPager.getCurrentItem() == 0) {
        //if tab 1 selected
        mDoubleBackToExitPressedOnce = true;
        if (mDoubleBackToExitPressedOnce)
            super.onBackPressed();
    }
    mViewPager.setCurrentItem(0);//go to tab 1
}

Solution 5:

I was also Struggling in this regard. after a lot of research i found a solution that we have to call super.onBackPressed() where our last or home fragment is Active

Solution

overridefunonBackPressed() {
    val activeFragment  = this.supportFragmentManager.
    findFragmentById(R.id.frame_layout)

    if (activeFragment is HomeFragment){
        startActivity(Intent(this, DashBoardActivity::class.java))
        finish()
        super.onBackPressed()
    }
    else
    {
        val ft: FragmentTransaction 
       =supportFragmentManager.beginTransaction()
      //should not call super.onBackPressed()

      switchFragment(ft)
    }
    //nor here you should call super.onBackPressed()
}

and the switchFragment function is

funswitchFragment(ft:FragmentTransaction) {
    try {
        ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
        if (supportFragmentManager.findFragmentById(R.id.frame_layout) == null) {
            ft.add(R.id.frame_layout, HomeFragment())
        } else {
            ft.replace(R.id.frame_layout, HomeFragment())
            println("Comess")
        }
        ft.addToBackStack(null)
        ft.commit()
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

Post a Comment for "Fragment Back Stack From Second Fragment To First Fragment"