Fragment Back stack from second fragment to first fragment

廉价感情. 提交于 2019-12-24 06:47:42

问题


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 backpress it exiting application as i have written code of Double back press exit code in my root fragment and i dont want this code to call in my another second fragment as i want simply one step back to my previous fragment As here is the code

 R.id.Recharge -> {

            val pl = Payment_History()

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


        }

In Payment history i am calling on Back press override function

override fun onBackPressed(): Boolean {
    super.onBackPressed()
}

and on clicking on Paymenthistory it called exit code from application. i want that it back to previous fragment. As I have written this fragment code but not working. Any one have idea how to back second nested fragment to previous fragment.

My OnBackPress code in my MainActivity

    override fun onBackPressed() {
    // TODO Auto-generated method stub
    try {
    if (getFragmentManager().getBackStackEntryCount() == 0) {

        if (doubleBackToExitPressedOnce) {

            //super.onBackPressed();
            val startMain = Intent(Intent.ACTION_MAIN)
            startMain.addCategory(Intent.CATEGORY_HOME)
            startMain.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            pref!!.setLoggedIn(true)
            startMain.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
            startMain.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
            startActivity(startMain)
            return
        }

        this.doubleBackToExitPressedOnce = true
        Toast.makeText(this, "Please click again to exit", Toast.LENGTH_SHORT).show()

        Handler().postDelayed({ doubleBackToExitPressedOnce = false }, 2000)
    }

        }catch (e:Exception){
        println("homemessage"+ e.message)
    }
}

回答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.




回答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(); 
    }
}



回答3:


if fragment within fragment back use this code onbackpreesed method

 @Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if(getFragmentManager().getBackStackEntryCount() == 1) {
        new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Check out")
                .setMessage("want to do check out?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(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




回答4:


I used this in Activity

Step 1:

Created a global variable for boolean

private boolean doubleBackToExitPressedOnce = false;

Step 2:

Then in onBackPress() method of activity

i did this

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


来源:https://stackoverflow.com/questions/45880745/fragment-back-stack-from-second-fragment-to-first-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!