0

In the navigation drawer I have a logout item and when I click on it I call a logout method but it's not executing any of the code inside this method.

This is the logout method, the first system.out.println does appear in the console but the one inside the onClick doesn't

    public void logout() {
        System.out.println("inside logout method");
        logoutBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                System.out.println("inside logout onclick");
                Call<Void> call = apiInterface.LogoutUser();

                call.enqueue(new Callback<Void>() {
                    @Override
                    public void onResponse(Call<Void> call, Response<Void> response) {
                        if (!response.isSuccessful()) {
                            FancyToast.makeText(MainActivity.this,
                                    "Ha sucedido un error, intente de nuevo.",
                                    FancyToast.LENGTH_SHORT, FancyToast.CONFUSING,
                                    R.drawable.error_outline, false).show();
                            return;
                        }
                        SharedPreferencesHelper.removeUser(MainActivity.this);

                        Intent i = new Intent(MainActivity.this, LoginActivity.class);
                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(i);

                    }

                    @Override
                    public void onFailure(Call<Void> call, Throwable t) {
                        FancyToast.makeText(MainActivity.this,
                                "Ha sucedido un error, intente de nuevo.",
                                FancyToast.LENGTH_SHORT, FancyToast.CONFUSING,
                                R.drawable.error_outline, false).show();
                        return;
                    }
                });


            }
        });

This is where I'm calling it, I do get the Toast message when I click on it

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.nav_calendar:
                getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, new CalendarFragment()).commit();
                break;
            case R.id.nav_survey:
                getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, new SurveyFragment()).commit();
                break;
            case R.id.nav_forum:
                getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, new ForumFragment()).commit();
                break;
            case R.id.nav_logout:
                logout();
                FancyToast.makeText(MainActivity.this,
                        "item clicked.",
                        FancyToast.LENGTH_SHORT, FancyToast.CONFUSING,
                        R.drawable.error_outline, false).show();
                break;
        }

        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

What am I doing wrong?

4
  • 1
    The only thing your logout() method is doing, basically, is setting the OnClickListener on logoutBtn. You would then need to click logoutBtn for the onClick() method to run. It's not really clear what setup you're trying to effect, there. Perhaps you want all of that network code directly inside logout(), and then to call logout() from logoutBtn's onClick(), too? Do you mean to have both a Button and a menu item to logout with? Commented Apr 13, 2020 at 19:32
  • @MikeM. I feel dumb now, I originally had a button but now I want to execute the logout function when the menu item is clicked Commented Apr 13, 2020 at 19:59
  • Happens to us all, occasionally. :-) You got it figured out, then? Commented Apr 13, 2020 at 20:01
  • 1
    @MikeM. yea, I just moved the code out of the btn onClickListener and everything is working now. Thanks! Commented Apr 13, 2020 at 20:02

1 Answer 1

1

Basically logoutBtn.setOnClickListener() will not get triggered because you are just initializing onClickListener when you are clicking on nav_logout.

if you want to initialize and click logoutBtn in logout().

then after logoutBtn.setOnClickListener() method add logoutBtn.perfomClick();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.