片段从第二个片段返回到第一个片段的堆栈 [英] Fragment Back stack from second fragment to first fragment

查看:71
本文介绍了片段从第二个片段返回到第一个片段的堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有viewpager选项卡片段,然后从按钮上的一个tabb片段中单击它,打开另一个片段和另一个第二个片段,我想添加backpress事件,因为我正在做backpress退出应用程序,因为我已经编写了Double back press退出代码在我的根片段中,我不希望此代码在另一个片段中调用,因为我只想退回到上一个片段 这是代码

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()
}

,然后单击Paymenthistory,它从应用程序中退出代码.我希望它回到以前的片段.由于我已经编写了此片段代码,但无法正常工作. 任何人都知道如何将第二个嵌套片段备份到上一个片段.

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.

MainActivity中的OnBackPress代码

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步:

为布尔值创建全局变量

private boolean doubleBackToExitPressedOnce = false;

第2步:

然后使用onBackPress()活动方法

Then in onBackPress() method of activity

我做到了

@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
}

这篇关于片段从第二个片段返回到第一个片段的堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆