There is an application with Firebase Auth, the signOut function lies in MainActivity, but the output must be implemented from MenuActivity, Intent does not work because the input is completed and the user value is initialized. It is necessary to call the signOut function from another activity, I do not know how to do this, please explain or tell where to read about it.

MainActivity

package com.example.ecohelp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.Toast; import android.app.ProgressDialog; import com.google.firebase.FirebaseApp; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; public class MainActivity extends Activity implements View.OnClickListener { private static final String TAG = "EmailPassword"; //Инициализация всего private EditText mEmailField; private EditText mPasswordField; public ProgressDialog pd; protected FirebaseAuth mAuth; @Override public void onCreate(Bundle savedInstanceState) { FirebaseApp.initializeApp(this); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Поля mEmailField = findViewById(R.id.fieldEmail); mPasswordField = findViewById(R.id.fieldPassword); // Кнопки findViewById(R.id.emailSignInButton).setOnClickListener(this); findViewById(R.id.emailCreateAccountButton).setOnClickListener(this); mAuth = FirebaseAuth.getInstance(); } //Проверка входа @Override public void onStart() { super.onStart(); FirebaseUser currentUser = mAuth.getCurrentUser(); updateUI(currentUser); } private void createAccount(String email, String password) { Log.d(TAG, "Создание аккаунта" + email); if (validateForm()) { return; } pd = new ProgressDialog(this); pd.show(); pd.setMessage("Регистрация"); //Регистрация через емайл mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, task -> { if (task.isSuccessful()) { pd.hide(); Log.d(TAG, "Аккаунт успешно создан"); FirebaseUser user = mAuth.getCurrentUser(); updateUI(user); } else { pd.hide(); Log.w(TAG, "Ошибка создания аккаунта", task.getException()); Toast.makeText(MainActivity.this, "Ошибка создания аккаунта", Toast.LENGTH_SHORT).show(); updateUI(null); } }); } private void signIn(String email, String password) { Log.d(TAG, "Вход" + email); if (validateForm()) { return; } pd = new ProgressDialog(this); pd.show(); pd.setMessage("Вход"); mAuth.signInWithEmailAndPassword(email, password) .addOnCompleteListener(this, task -> { if (task.isSuccessful()) { pd.hide(); Log.d(TAG, "Вход через почту успешен"); FirebaseUser user = mAuth.getCurrentUser(); updateUI(user); } else { pd.hide(); Log.w(TAG, "Вход не вошелся", task.getException()); Toast.makeText(MainActivity.this, "Ошибка входа", Toast.LENGTH_SHORT).show(); updateUI(null); } }); } private boolean validateForm() { boolean valid = true; String email = mEmailField.getText().toString(); if (TextUtils.isEmpty(email)) { mEmailField.setError("Пусто"); valid = false; } else { mEmailField.setError(null); } String password = mPasswordField.getText().toString(); if (TextUtils.isEmpty(password)) { mPasswordField.setError("Пусто"); valid = false; } else { mPasswordField.setError(null); } return !valid; } protected void signOut() { mAuth.signOut(); updateUI(null); } private void updateUI(FirebaseUser user) { pd = new ProgressDialog(this); pd.hide(); if (user != null) { Intent intent = new Intent(MainActivity.this, Menu.class); startActivity(intent); } } @Override public void onClick(View v) { int i = v.getId(); if (i == R.id.emailCreateAccountButton) { createAccount(mEmailField.getText().toString(), mPasswordField.getText().toString()); } else if (i == R.id.emailSignInButton) { signIn(mEmailField.getText().toString(), mPasswordField.getText().toString()); } } 

MenuActivity

 import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class Menu extends Activity implements View.OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_menu); } public void onClick(View v) { int i = v.getId(); if (i == R.id.map) { Intent intent = new Intent(this, MapsActivity.class); startActivity(intent); } else if (i == R.id.signOut) { // Как реализовать выход из аккаунта? } } } 

MenuActivity.xml

 <Button android:id="@+id/map" android:layout_width="164dp" android:layout_height="61dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:backgroundTint="@color/maps" android:text="@string/map" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.451" /> <Button android:id="@+id/signOut" android:layout_width="88dp" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:text="@string/signOut" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> 

ActivityMain.xml

 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TableRow android:layout_width="422dp" android:layout_height="359dp"> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/aaaaaaaaaaa" /> </TableRow> <LinearLayout android:layout_width="434dp" android:layout_height="225dp" android:orientation="vertical"> <EditText android:id="@+id/fieldEmail" android:layout_width="match_parent" android:layout_height="87dp" android:ems="10" android:hint="@string/email" android:inputType="textEmailAddress" /> <EditText android:id="@+id/fieldPassword" android:layout_width="match_parent" android:layout_height="84dp" android:ems="10" android:hint="@string/password" android:inputType="textPassword" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="69dp" android:orientation="horizontal"> <Button android:id="@+id/emailCreateAccountButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/createAccount" /> <Button android:id="@+id/emailSignInButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/signIn" /> </LinearLayout> </LinearLayout> 
  • Are you sure that signOut () should be in MineActivity? Maybe you should move it to the menu? Well, if you don’t have one, then you need a MainActivity instance and have it pull the method, or make the method static and call MainActivity.signOut () - Eugene Zaychenko
  • you can still make a Basic Activation from it inherit all other activations and in the basic implement all the necessary methods for general use with protected access - Eugene Zaychenko
  • Thanks, but implemented it like this - FirebaseAuth.getInstance (). SignOut (); - georgiy110802

1 answer 1

 FirebaseAuth.getInstance().signOut()