Search code examples
androidonbackpressed

Phones back button press responds only after 3-5 times tap?


I have an fragment inside activity, which has nested full-screen, they are made like nested screens so I am handling back press in my container activity as follows :

    @Override
            public void onBackPressed() {
                DrawerLayout drawer = (DrawerLayout) 
    findViewById(R.id.drawer_layout); // first line where I have debug point
                if (drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(GravityCompat.START);
// If drawer is open then on backpress drawer will be closed
                } else {
                    if (docFragment != null && docFragment.isAdded()) {
                        if (docFragment.isNestedViewVisible() == View.VISIBLE) {
                             docFragment.closeNestedView();
        // Above method will hide the nested view in fragment 
        // & return back to original default view of fragment
                        } else {
                            super.onBackPressed();
                        }
                    } else {
                        super.onBackPressed();
                    }
                }
            }

The code using which I am adding a fragment :

docFragment = DocumentFragment.newInstance();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.container, docFragment, "Documents");
        fragmentTransaction.commit();

Some times phones back button don't have any effect. I have to press it 3-5 times. I ran the application in debugger but debugger also gets on first line after 3-5 times tap.

This is handled from activity.

But why is that my onBackPress event is getting fired after 3-5 times tapping, not in single click ?

Update :

User Maik Peschutter below has suggested that my fragment may have some heavy process which may cause the onBackPress delivery to delay or not happen at all.

I investigated in that percepective it appeared that there was one service in background which was sending some signals ( Broadcasts ) which were causing my UI to redrawn, The component getting redrawn is complex and takes time.

The frequency of signals from background service was too much, in millisecods it caused the redrawn to happen rapidly which made my app freez, Now I have scheduled service to send the signal at an interval of 20 seconds. This solved my problem. Now the tap responds on first time & far more quickly than earlier.


Solution

  • Try to debug your application. Do a breakpoint in the first line of your onBackPressed() method in your activity. So you can be sure if the method gets invoked after the first tap on your phones back button or not.

    If not, maybe the FragmentManager already handled the event and removes a fragment from backstack each time you tap on your button.

    Maybe your docFragment.closeNestedView() method is very slow so that docFragment.isNestedViewVisible() is returning not View.VISIBLE right after you called docFragment.closeNestedView(). Hard to say without the code.