0

I have more activities in my project. I have added logout button to log out user to registration page. Now iam going to add exit button i have added the button but when i click it not exit the app instead opening the registration activity. I want exit all activities How?

package com.weatherapp;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MenuActivity extends AppCompatActivity {

    private Button btnGoto;
    private Context context1;

    private Button btngoto1;
    private Context context2;

    private Button btngoto2;
    private Context context3;

    Button LogOut;
    public static final String SHARED_PREFS ="sharedPrefs";

    private Button closeApplicationBtn;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        LogOut = findViewById(R.id.logOut);

        LogOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();

                editor.putString("name", "");
                editor.apply();


                Intent intent2 = new Intent(getApplicationContext(),Register.class);
                startActivity(intent2);
            }
        });

        context3=this;
        btngoto2 = findViewById(R.id.button4);
        btngoto2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent3= new Intent(context3,NewsActivity.class);
                startActivity(intent3);
            }
        });






        context2=this;
        btngoto1 = findViewById(R.id.button5);
        btngoto1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2 = new Intent(context2,IMDACTIVITY.class);
                startActivity(intent2);
            }
        });




        context1=this;
        btnGoto = findViewById(R.id.button3);
        btnGoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(context1,HomeActivity.class);
                startActivity(intent1);
            }
        });




        // on below line we are initializing variables with ids.
        closeApplicationBtn = findViewById(R.id.exit);

        // on below line we are adding click listener for our button
        closeApplicationBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // on below line we are finishing activity.
                finish();
            }
        });
    }


}

I tried adding finsh() function.

Progman
  • 16,827
  • 6
  • 33
  • 48
Sriram S
  • 11
  • 1
  • 2
    "when i click it not exit the app instead opening the registration activity" -- perhaps you should `finish()` the registration activity when you are done with it. Or, perhaps this should be a single activity with two fragments or composables representing the screens. – CommonsWare Mar 26 '23 at 16:03
  • Now it goes to the login activity. I have added the finish() in both register and login activity but it doesn't exit the app – Sriram S Mar 26 '23 at 17:30

0 Answers0