0

Every posts are for navigation drawer but as I am using Bottom navigation I couldn't find any solutions. searched and tried all threads.

This is my selector method for selecting the menu items

public boolean onNavigationItemSelected(@NonNull MenuItem item) 

          {

            View v=null;

            int id = item.getItemId();

            switch (id){

                case R.id.search:
                    fragment = new Search();

                    break;
                case R.id.todo:
                    fragment = new ServiceTable();
                    break;
                case R.id.info:
                    fragment = new Orderlist();
                    break;
                case R.id.close:

                    //have to implement double click here.

                    break;

            }

            final FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.main_container, fragment).commit();
            return true;
        }


    });

    if (savedInstanceState == null) {
        bottomNavigation.setSelectedItemId(R.id.search);
    }
}
Vishnu Roshan
  • 25
  • 2
  • 12

3 Answers3

2

You have to make a global boolean variable to check the double click functionality like :

boolean doubleBackToExitPressedOnce = false;

after this implement the following code in your case R.id.close

if (doubleBackToExitPressedOnce) {
            finishAffinity();
        }
        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                doubleBackToExitPressedOnce = false;
            }
        }, 2000);
Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40
1

you can write

boolean isClickedTwice = false;
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        View v = null;
        int id = item.getItemId();
        switch (id){
            case R.id.close:

                //have to implement double click here.
                if (isClickedTwice) {
                  this.finish();
                }
                isClickedTwice = true;

                break;

        }

        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.main_container, fragment).commit();
        return true;
    }


});
mzennis
  • 103
  • 1
  • 5
0

I see you're using fragments. are you maintaining any back stack trace for this? I suggest you do and then you implement double-click to exit the app. let me know

allowedToExit is a boolean to keep track of user if he is allowed to exit or not. that we judge based on the number of stacked fragments and if user pressed two times. i used this logic in my onBackPressed method to create a two time back press exit app

//fragments remove logic
        int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
        // this is last item
        if (backStackEntryCount == 1) {
            if (allowedToExit)
                finish();
            else {
                allowedToExit = true;
                Util.showToast(BaseActivity.this,
                        "Press again to exit", false);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        allowedToExit = false;
                    }
                }, 1000);
                return;
            }
        }
        // we have more than 1 fragments in back stack
        if (backStackEntryCount > 1) {
            Util.hideSoftKeyboard(BaseActivity.this);
            onRemoveCurrentFragment();
        } else
            super.onBackPressed();
Usman Ghauri
  • 931
  • 1
  • 7
  • 26