onBackPressed function not working under IMMERSIVE STICKY mode

1.1k views Asked by At

I'm using Immersive Mode for an activity with videoView inside.

My goal is when touch on screen, the media controller and the system control bar show or disappear together. Everything works fine now.

The problem is I can't leave activity properly. When I press the back button once, the system bar just hide again and nothing happens. I have to press twice to exit the activity. I don't know why.

Here's my code.

I use an FullScreenActivity() activity to define IMMERSIVE MODE:

@TargetApi(Build.VERSION_CODES.KITKAT)
private void FullScreenActivity() {
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav                                                               // bar
                        | View.SYSTEM_UI_FLAG_FULLSCREEN// hide status bar;
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
}

in OnCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {

    //getActionBar().setDisplayShowTitleEnabled(false);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    FullScreenActivity();

And in the media controller, I define the IMMERSIVE MODE again, because if I don't after the controller shows, the actionbar and system bar will be back again.

private class MyController extends MediaController {

    public MyController(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public boolean onTouchEvent(MotionEvent event) {
        //........
    }

    @Override
    public void show() {
        super.show();
        FullScreenActivity();
    }

    @Override
    public void hide() {
        super.hide();
        FullScreenActivity();
    }

}

and at last, in the onBackPressed(), I implemented my function:

@Override
public void onBackPressed() {
    Toast.makeText(getApplicationContext(), "onbackpressed", Toast.LENGTH_SHORT).show();
    super.onBackPressed();

    if (tgtIDArr != null && tgtIDArr.size() > 0) {
        new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Exit Player!")
                .setMessage("Are you sure you want to leave Player?")
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                .......
                                finish();
                            }
                        }).setNegativeButton("No", null).show();
    }else{
        ........            
        finish();
    }
}  

I don't know why, but only when I press back button twice would trigger this onBackPressed() function.

Anyone knows why? thanks!!

1

There are 1 answers

1
Ryan On BEST ANSWER

If you want to exit the activity when the back button is pressed and the media controller is visible then you can intercept the back button with the following code snippet.

   @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int keyCode = event.getKeyCode();
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

Which is the same solution to a similar problem here Back button not working in Media player