如何在单一背景上返回家庭活动

时间:2016-10-07 10:34:44

标签: android android-fragments onbackpressed

我想回到家庭活动的单一背景和双重背景退出应用程序,但它没有正常工作我已经添加下面的代码,请帮助我。

 if (doubleBackToExitPressedOnce) 
{
            replaceFragment(new HomeActivity());
            super.onBackPressed();
            return;
        }

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

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                doubleBackToExitPressedOnce = false;
            }
        }, 2000);

请帮助我。

2 个答案:

答案 0 :(得分:0)

只需在您的处理程序运行方法

中添加它
            @Override
            public void run() {
                replaceFragment(new HomeActivity());
                ActivityName.super.onBackPressed();
                doubleBackToExitPressedOnce = false;
            }

制作像这样的其他方法

else{
replaceFragment(new HomeActivity());
            super.onBackPressed();
}

答案 1 :(得分:0)

private static final int BACK_PRESS = 76;

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (BACK_PRESS == msg.what) {
            goBack();
        }
    }
};

private void goBack() {
    super.onBackPressed();
}

@Override
public void onBackPressed() {
    if (mHandler.hasMessages(BACK_PRESS)) {
        mHandler.removeMessages(BACK_PRESS);
        replaceFragment(new HomeActivity());
    } else {
        mHandler.sendEmptyMessageDelayed(BACK_PRESS, 500); // Your activity will close after 500ms
    }
}

你可以以不同的方式使用处理程序。