Danya Alhadad Danya Alhadad - 9 days ago 8
Android Question

How to I go to next activity after login with Google?

After success login with Google, I would like go to next activity, how can I do that? This all my code, I used Firebase in order to login



package com.example.asus.myapplication;

import android.content.Intent;
import android.graphics.drawable.Icon;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.telecom.GatewayInfo;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.Api;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.firebase.auth.FirebaseAuth;
import com.squareup.picasso.Picasso;

import static com.example.asus.myapplication.R.id.sign_google;


public class sign_in_screen extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener,View.OnClickListener {
GoogleApiClient mGoogleApiClient;
SignInButton signInButton;
Button but_sing_out;
private TextView textview;
private ImageView ImgAvatar;
private static final int RC_SIGN_IN =9001;
private static final String TAG ="signInActivity";
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener muAuthstateListener;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in_screen);



Toolbar my_toolbar= (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(my_toolbar);
getSupportActionBar().setTitle(R.string.tb_title0);
getSupportActionBar().setIcon(R.drawable.ic_toolbar);

GoogleSignInOptions gso=new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient=new GoogleApiClient.Builder(this)
.enableAutoManage(this,this)
.addApi(Auth.GOOGLE_SIGN_IN_API,gso)
.build();

textview=(TextView)findViewById(R.id.text1);
ImgAvatar=(ImageView)findViewById(R.id.imag_avatar);
signInButton=(SignInButton)findViewById(sign_google);
but_sing_out=(Button)findViewById(R.id.but_sing_out);
signInButton.setOnClickListener(this);
but_sing_out.setOnClickListener(this);



}


@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.sign_google:
signIn();
break;
case R.id.but_sing_out:
singout();
break;

}

}



private void signIn() {
Intent signInIntent=Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent,RC_SIGN_IN);

}


@Override
public void onActivityResult(int requestcode,int resultcode,Intent data) {
super.onActivityResult(requestcode,resultcode,data);
if (requestcode==RC_SIGN_IN){
GoogleSignInResult result=Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);


}

}

private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG,"handleSignInResult:"+ result.isSuccess());
if (result.isSuccess()){
GoogleSignInAccount acc=result.getSignInAccount();
textview.setText("Welcome"+ acc.getDisplayName()+"\n"+"\n"+":email"+ acc.getEmail());
try {
String PohtoUlr=acc.getPhotoUrl().toString();
Picasso.with(sign_in_screen.this).load(PohtoUlr).into(ImgAvatar);

} catch (Exception e){

}
}
}


@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(TAG,"onConnectionFailed:"+connectionResult);


}

private void singout() {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
Toast.makeText(sign_in_screen.this,"LOG OUT",Toast.LENGTH_SHORT).show();

}
});
}
}




Answer

Just use intent to move to next page and you should include it after the catch statement.

Intent intent = new Intent(this, Nextpage.class);

startActivity(intent);

Comments