Unable to Exit App Using Drawer Menu "Exit" Option in Android App

64 Views Asked by At

Description:

I am developing an Android app, and I have encountered an issue with the "Exit" option in the drawer menu. When I click on the "Exit" option, nothing happens, and the app doesn't quit. I have implemented several Log.d statements to debug the problem.

Here is the code for my WelcomeActivity:

package com.enetapplications.songtracker;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class WelcomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        Log.d("WelcomeActivity", "WA: onCreate method (line 23)");
        Log.d("ActivityTracker", "WelcomeActivity is created");

        // Get references to the TextViews
        TextView welcomeText = findViewById(R.id.welcomeText);
        TextView userNameText = findViewById(R.id.userNameText);
        TextView userEmailText = findViewById(R.id.userEmailText);
        TextView userIdText = findViewById(R.id.userIdText);

        // Retrieve user data from intent extras
        Intent intent = getIntent();
        String userName = intent.getStringExtra("USER_NAME");
        String userEmail = intent.getStringExtra("USER_EMAIL");
        int userId = intent.getIntExtra("USER_ID", -1); // Use "USER_ID" as the key name

        // Set the values in TextViews
        welcomeText.setText("Welcome " + userName);
        userNameText.setText("User Name: " + userName);
        userEmailText.setText("Email: " + userEmail);
        userIdText.setText("User ID: " + userId);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.drawer_menu, menu);
        Log.d("WelcomeActivity", "WA: Menu created (line 47)");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        Log.d("WelcomeActivity", "WA: just entering onOptionsItemSelected (line 52)");
        switch (item.getItemId()) {
            // Handle other menu items if any

            case R.id.menu_item_exit:
                // Add code here to perform logout or cleanup tasks if needed
                Log.d("WelcomeActivity", "WA: Exit menu item clicked");
                finish(); // This will close the app
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

For the drawer_menu.xml file, here is the relevant section where I have defined the "Exit" option:

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <!-- Header: About -->
    <item
        android:id="@+id/menu_item_about_header"
        android:title="About"
        android:enabled="false" />

    <!-- About Menu Group -->
    <group android:id="@+id/group_about">
        <item
            android:id="@+id/menu_item_app_information"
            android:title="App Information" />
        <item
            android:id="@+id/menu_item_credits"
            android:title="Credits" />
        <item
            android:id="@+id/menu_item_exit"
            android:title="Exit" />
    </group>
</menu>

I am trying to get the "Exit" option to quit however nothing happens when I click on the "Exit" in the drawer_menu. I have placed numerous Log.d tags and know when I have arrived at the WelcomeActivity page however only the Log.d in the onCreate method is reached. No errors are thrown and app seems to run ok, however I cannot figure out why the "Exit" is not working.

Thank you advance for any assistance you may provide!

best, Brian

0

There are 0 best solutions below