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.