0
這是我在主要活動中的註銷按鈕,它工作完美。順便說一句,我使用的是Firebase,這是Google登錄的註銷按鈕。當它在我的主課堂時,它按照預期工作。註銷按鈕在從活動移動到片段後不起作用
package com.example.akhilsukh01.truckory;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.firebase.auth.FirebaseAuth;
public class HomeActivity extends AppCompatActivity {
Button button;
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthListener;
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
button = (Button) findViewById(R.id.logout);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() == null) {
startActivity(new Intent(HomeActivity.this, MainActivity.class));
}
}
};
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAuth.signOut();
}
});
}
}
我知道,因爲它不是一個活動,這是一個不同的片段完全不會使用相同的代碼工作,但我不知道如何解決它。這是我的嘗試(這是行不通的):
package com.example.akhilsukh01.truckory;
/**
* Created by akhil on 7/24/2017.
*/
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.firebase.auth.FirebaseAuth;
public class Tab5Profile extends Fragment {
Button button;
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthListener;
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.tab5profile, container, false);
return rootView;
Button logout = (Button) rootView.findViewById(R.id.logout);
logout.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Logout current user
mAuth.signOut();
startActivity(new Intent(HomeActivity.this, LoginActivity.class));
}
});
}
}
謝謝,我究竟在哪裏,但是,這就是我所做的。 https://jpst.it/123Fe –
把它放在logout.setOnclickListener()中的片段 – FnR
非常感謝你 –