有一个具有Firebase Auth的应用程序,注销功能位于MainActivity中,但是必须从MenuActivity实现输出,Intent无法正常工作,因为已执行输入并初始化了用户值。需要在其他活动之外调用函数sign,我不知道该怎么做,请解释或告诉该信息。
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>
答案 0 :(得分:1)
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) {
FirebaseAuth.getInstance.signOut();
Intent navigateIntent=new Intent(CurrentActivity.this,SecondActivity.class);
startActivity(navigateIntent);
//so that when you click the back button, you won't have to able to go back the previous activity
finish();
}
}
}