【问题标题】:Where to add code for onBackPressed() for Fragements在哪里为片段添加 onBackPressed() 代码
【发布时间】:2018-01-12 20:39:36
【问题描述】:

我有一个只有一个活动但有 3 个片段的应用。

现在我找到了一种方法(Youtube 视频),可以在点击手机上的后退按钮后停止应用完全退出。

boolean twice = false;
@Override
public void onBackPressed() {

    Log.d(TAG, "click");

    if(twice == true){
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
        System.exit(0);
    }
    twice = true;
    Log.d(TAG, "twice: " + twice);

//        super.onBackPressed();
    Toast.makeText(MainActivity.this, "Tap twice to exit", Toast.LENGTH_SHORT).show();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            twice = false;
            Log.d(TAG, "twice: " + twice);
        }
    }, 2000);

}

另一个线程显示使用后退按钮在片段之间“导航”的代码 How to implement onBackPressed() in Fragments?

这是代码

@Override

public void onBackPressed() {

int count = getFragmentManager().getBackStackEntryCount();

if (count == 0) {
    super.onBackPressed();
    //additional code
} else {
    getFragmentManager().popBackStack();
}

}

当我在第一个代码行下添加时,应用程序在点击手机上的后退按钮后退出完成。有人能告诉我在哪里添加第二个代码行,这样两个东西都可以工作吗?

提前非常感谢

【问题讨论】:

    标签: android android-fragments onbackpressed


    【解决方案1】:

    更新 这是我在使用 Fragment Transaction 将 Fragment Entry 添加到 BackStack 时所做的操作

    ReviewOrder fragment = new ReviewOrder();
                    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.setCustomAnimations(R.anim.slide_in_up,  
                            R.anim.slide_out_up); //OPTIONAL For Animations
                    fragmentTransaction.replace(R.id.tabframelayout,fragment,FRAGMENT_TAG);
    
                    //Add This To Your Code To Add To BackStack
                    fragmentTransaction.addToBackStack(FRAGMENT_TAG);
    
    
                    fragmentTransaction.commit();
    

    您可以做的是仅在 (count == 0) 时执行双击逻辑(第一个代码),这样如果后台堆栈中有片段,您的应用将不会在后台退出时退出

    boolean twice = false;
    @Override
    public void onBackPressed() {
    
    int count = getFragmentManager().getBackStackEntryCount();
    
    if (count == 0) {
        Log.d(TAG, "click");
    
        if(twice == true){
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            super.onBackPressed();
        }
        twice = true;
        Log.d(TAG, "twice: " + twice);
    
        Toast.makeText(MainActivity.this, "Tap twice to exit", 
            Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                twice = false;
                Log.d(TAG, "twice: " + twice);
            }
        }, 2000);
    } else {
        getFragmentManager().popBackStack();
    }
    }
    

    【讨论】:

    • 不幸的是它总是返回 0,所以它总是说“点击两次退出”。它永远不会“跳”回到最后一个片段。您需要我使用的更多详细信息吗,请告诉我
    • 看看更新后的答案
    • 我还有最后一个问题。我不知道在哪里添加“ReviewOrder”。我必须将它添加到 MainActivity.java 或 StandortFragment.java 中吗?那么在每个 Fragment 中还是在 MainActivity.java 中?
    • ReviewOrder 将被您的片段替换
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多