0

So I have been working on a little project of my own, The problem I am facing is It is working but the startActivity is executing before the signIn() method is done executing, i.e, after completing the activity I am getting a popup for selection of the Email account.

@Override
public void onClick(View v) {
    if (v.getId() == R.id.sign_in_button) {
        signIn();
    }
    startActivity(new Intent(LoginActivity.this, MainActivity.class));
    overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
    finish();
}

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

And, the app is not working when the internet is not available, how do I make it to work? It crashes with the message "Unfortunately App has stopped."

Yash Agarwal
  • 57
  • 1
  • 6
  • If signIn is running asynchronously (which it must be if networking is involved) you will need to implement a callback interface that waits for a response and then starts the activity. – zgc7009 May 31 '16 at 16:32
  • I din't thought of threading or Asynctask as I don't have any experience with it but thanks anyway I will try to implement it in AsyncTask as provided in the answer below – Yash Agarwal May 31 '16 at 17:05
  • Mind posting your code for signIn()? I might be able to help a bit more if you are still having trouble – zgc7009 May 31 '16 at 17:15
  • private void signIn() { Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); } – Yash Agarwal May 31 '16 at 17:44
  • It isn't a race case, it is an if/else thing. Your logic is off. – zgc7009 May 31 '16 at 18:54
  • @zgc7009 My logic is off, Can you explain it a further more? – Yash Agarwal Jun 01 '16 at 08:01
  • This could be solved by doing this `if (v.getId() == R.id.sign_in_button) signIn(); else startActivity(new Intent(LoginActivity.this, MainActivity.class));` then finish it off with your overridePendingTransition call and your finish call. The way you have it now stacks activities. – zgc7009 Jun 01 '16 at 12:52

2 Answers2

0

Refer this Check network connectivity for android , do this before doing any network operations and notify user with the dialogbox if not connected to internet .

Regarding the activity starts before signIn() function is complete , i would recommend to use AsyncTask() for this ,

  • in onpreexceute() you can show the the progress dialog .
  • in doinBackground() put signIn() function
  • in onpostexeceute() start the activity

for AsyncTask Referece Refer here .

Good Luck .

Community
  • 1
  • 1
  • Choose this answer as correct if you find this helpful and has resolved your issue :) . – Mohit Uppal May 31 '16 at 16:50
  • I did that for the net connection the problem is the app crashes when trying to open the app without the internet connection. – Yash Agarwal May 31 '16 at 17:04
  • can u post the error log image of android studio or some info on error that causing the app to crash...... – Mohit Uppal May 31 '16 at 19:37
  • I was able to implement AsyncTask now it works only problem is the app crashes when internet is not available. – Yash Agarwal Jun 01 '16 at 10:20
  • can u post the error log image of android studio or some info on error that causing the app to crash...... u can find the details of the error in android monitor in android studio . – Mohit Uppal Jun 02 '16 at 05:17
0

try to catch exception ( which exception you get )

private void signIn() {
    try{
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }
    catch(Exception ex){
        //write to us your ex.getMessage();
    }
}
ugali soft
  • 2,719
  • 28
  • 25