是否可以仅针对活动制作onBackPressed,而不是针对包含它的片段?

时间:2016-08-12 11:44:33

标签: android android-fragments

我有一个活动,其他是包含它的片段。我的fragment_container是一个活动。 Activity是我的Main Activity.I在Activity中调用fragmentTransaction并替换。但当我按下硬件按钮时,它会直接关闭我的应用程序,所以我使用警告提醒对话框。现在的问题是,当我按下后退按钮时会弹出对话框?有没有什么办法解决这一问题?以下是我的代码:

 @Override
public void onBackPressed() {
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
    builder.setTitle("Quit App?").setCancelable(true).setPositiveButton("yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
             finish();
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    }).show();
}

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.img_boy:
            cardView cardview=new cardView();
            FragmentManager fragmentManager=getFragmentManager();
            FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fragment_container,cardview);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
            break;   
    }
}

}

2 个答案:

答案 0 :(得分:0)

这是因为您未正确修改onBackPressed。首先,您需要获取backstack FragmentManager中的片段数量并采取相应的操作。

以下是您案例中的示例:

@Override
public void onBackPressed() {
        // this will get the fragments in the backstack, Replace `getFragmentManager` with `getSupportFragmentManager` if your using `v4.app.Fragment` 
    int count = getFragmentManager().getBackStackEntryCount();

    switch(count){
        case 0:
             AlertDialog.Builder builder=new AlertDialog.Builder(this);
                    builder.setTitle("Quit App?").setCancelable(true).setPositiveButton("yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            finish();
                        }
                    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    }).show();
            break;
        default:
            //Replace `getFragmentManager` with `getSupportFragmentManager` if your using `v4.app.Fragment` here too
            getFragmentManager().popBackStack();
            break;
    }
}

答案 1 :(得分:0)

是的,你可以像这个例子那样处理片段背压

handleBackPressed(view);//view is fragment layout

//setKey Listener
 private void handleBackPressed(View view)
    {
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        view.setOnKeyListener(this);
    }

//handle key listener

  @Override
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        if( keyCode == KeyEvent.KEYCODE_BACK )
        {
           //handle your code here...
        }
    }