0

I have this activity:

public class AMLoginActivity extends Activity implements IAsyncResponse {

    public static int finished;


    private final String TAG = "AMLoginActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        finished = 0;

        Intent i = new Intent(AMLoginActivity.this, GDLoginActivity.class);
        i.putExtra("response", this);
        AMLoginActivity.this.startActivity(i);


    }

    public void processFinish(){
        finished++;
        Log.i(TAG, "Number finished: " + finished);


        if(finished == 1){
            Log.i(TAG, "All finished");
            AMLoginActivity.this.finish();
            Log.i(TAG, "Finish called");
        }

    }

Which calls the GDLoginActivity to login to google drive. It can be seen below:

public class GDLoginActivity extends Activity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    private static final String TAG = "GDLoginActivity";
    private static final int REQUEST_CODE_RESOLUTION = 3;

    private static GoogleApiClient mGoogleApiClient;


    @Override
    protected void onResume() {
        super.onResume();
        if (mGoogleApiClient == null) {
            // Create the API client and bind it to an instance variable.
            // We use this instance as the callback for connection and connection
            // failures.
            // Since no account name is passed, the user is prompted to choose.
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
            Log.i(TAG, "New GDClient created");
        }
        // Connect the client.
        mGoogleApiClient.connect();

    }

    @Override
    protected void onPause() {
        if (mGoogleApiClient != null) {
            mGoogleApiClient.disconnect();
        }
        super.onPause();
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "API client connected.");

        IAsyncResponse response = (IAsyncResponse) getIntent().getSerializableExtra("response");
        Log.i(TAG, "Process finish");
        response.processFinish();
        finish();
    }

    @Override
    public void onConnectionSuspended(int cause) {
        Log.i(TAG, "GoogleApiClient connection suspended");
    }


    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // Called whenever the API client fails to connect.
        Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
        if (!result.hasResolution()) {
            // show the localized error dialog.
            GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
            return;
        }
        // The failure has a resolution. Resolve it.
        // Called typically when the app is not yet authorized, and an
        // authorization
        // dialog is displayed to the user.
        try {
            result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
        } catch (IntentSender.SendIntentException e) {
            Log.e(TAG, "Exception while starting resolution activity", e);
        }
    }


    public static  GoogleApiClient getGoogleApiClient(){
        return mGoogleApiClient;
    }

}

The issue is that when the GDLoginActivity connects and finish()'s, it should increment finish by 1 and and also finish the AMLoginActivity. It does increment by 1 and call finish() in processFinish(), but nothing happens and AMLoginActivity doesn't actually close (i.e. onDestroy() is never called), so i'm just left with a blank screen. If I remove GDLoginActivity and just call processFinish() instead, then AMLoginActivity finishes just fine, so I assume it has something to do with GDLoginActivity, but this is happening with other similar activities too. Any ideas?

Edit: Also if I hit the back button on the blank screen, then it calls the onDestroy() method of AMLoginActivity and goes to the activity I want, if that hints a clue at what is going on?

Niamh
  • 33
  • 1
  • 5

1 Answers1

0

It looks like you're finishing them in a weird order. Try finishing the visible activity before you finish the one that started it.

  @Override
    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "API client connected.");

        IAsyncResponse response = (IAsyncResponse) getIntent().getSerializableExtra("response");
        Log.i(TAG, "Process finish");
        //finishes the previous activity
        response.processFinish();

        //finishes the visible activity
        finish();

        //try flipping this order ^
    }

You could use startActivitForResult() if you're trying to finish one activity after another has finished.

Tyler Pfaff
  • 4,900
  • 9
  • 47
  • 62
  • Switching them round didn't seem to change anything. I wanted to use startActivityForResult() but some of the activities work asynchronously, so it was returning the result before the activity had actually finished. – Niamh May 02 '16 at 09:29
  • Why is GDLoginActivity even an activity? It has no ui it seems. Make it a service or an asynctask. – Tyler Pfaff May 02 '16 at 21:54