我正在用我的android应用片段集成Google登录。但是当我登录它时,打开一个新的片段b。但是当片段b当我按位置登录按钮时,它再次显示登录窗口
我已经使用fragmentTransaction.addToBackStack(null);
登录片段(片段a)
public class LoginFragment1 extends Fragment {
int RC_SIGN_IN = 9001;
GoogleSignInClient mGoogleSignInClient;
SignInButton signInButton;
LoginButton loginButton;
LinearLayout linearLayout,linearLayout1;
CallbackManager callbackManager;
private static final String EMAIL = "email";
@Override
public void onStart() {
super.onStart();
GoogleSignInAccount account =
GoogleSignIn.getLastSignedInAccount(getActivity());
if(account!=null){
updateUI(account);
}
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new
FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "onSuccess: "+loginResult );
}
@Override
public void onCancel() {
Log.d(TAG, "onCancel: ");
}
@Override
public void onError(FacebookException error) {
Log.e(TAG, "onError: ",error );
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
GoogleSignInOptions gso = new
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
View view = inflater.inflate(R.layout.fragment_login_fragment1,
container, false);
//seting apps name
TextView textView = view.findViewById(R.id.textView6);
String text = "Lets<font color='#faa71a'>Go</font>";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY),
TextView.BufferType.SPANNABLE);
} else {
textView.setText(Html.fromHtml(text),
TextView.BufferType.SPANNABLE);
}
//google login
linearLayout = view.findViewById(R.id.linearLayout);
signInButton = view.findViewById(R.id.signInButton);
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signInButton.performClick();
signIn();
}
});
mGoogleSignInClient = GoogleSignIn.getClient(getActivity(), gso);
//facebook login
linearLayout1 = view.findViewById(R.id.linearLayout2);
loginButton = view.findViewById(R.id.loginButton);
CallbackManager callbackManager = CallbackManager.Factory.create();
linearLayout1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loginButton.performClick();
}
});
loginButton.setFragment(this);
loginButton.setReadPermissions(Arrays.asList("email", "public_profile", "user_friends"));
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
updateUI(account);
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
updateUI(null);
}
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
private void updateUI(GoogleSignInAccount account) {
Toast.makeText(getActivity(),"signedin" ,Toast.LENGTH_SHORT).show();
Fragment fragment = new StepFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Toast.makeText(getActivity(),
account.getDisplayName()+"\n"+account.getEmail(),
Toast.LENGTH_SHORT).show();
}
}
用户片段(片段b)
public class StepFragment extends Fragment {
Button Logoutl;
GoogleSignInClient mGoogleSignInClient;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_step, container, false);
GoogleSignInOptions gso = new
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleSignInClient = (GoogleSignInClient) GoogleSignIn.getClient(getActivity(), gso);
Logoutl = view.findViewById(R.id.button2);
Logoutl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mGoogleSignInClient.signOut();
}
});
Fragment fragment = new LoginFragment1();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.detach(fragment);
fragmentTransaction.commit();
return view;
}
}
答案 0 :(得分:0)
在内部登录片段中为登录片段创建TAG名称,例如public static String TAG = "Login";
在Second Fragment内创建一个函数,如下所示
public void removeFragmentByTag(String tag) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);
if (fragment != null) {
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
}
}
在“第二个片段”中,通过传递登录片段TAG名称(例如)来调用此方法
removeFragmentByTag(Login.TAG);
它将删除登录片段
答案 1 :(得分:0)
另一种方法是使用Otto之类的库在事件总线上发送事件。由于片段的固有性质,使用add方法添加的所有彼此重叠的片段都是活动的,不会暂停。...因此,您触发的任何事件都会传递所有活动的侦听器。
创建一个事件类
public class CloseFrag{}
在需要的片段中声明事件总线
private static final Bus eventBus = new Bus(ThreadEnforcer.ANY);
在需要的片段的onResume中注册事件总线:
@Override
public void onResume() {
eventBus.register(this);
super.onResume();
}
在需要的片段的onPause中取消注册事件总线
@Override
public void onPause() {
eventBus.unregister(this);
super.onPause();
}
存在Google登录信息的片段中,一旦登录完成便触发该事件
eventBus.post(new CloseFrag());
在要关闭的片段中,注册总线后收听事件
@Subscribe
public void event(CloseFrag event){
//Take necessary action eg popBackStack() or getActivity().finish() as per your need
}