@Override
  public void onResume() {
    super.onResume();

    if (!authorizeCalled_) {
      try {
        GDAndroid.getInstance()
            .authorize(
                new GDAppEventListener() {

                  private boolean uiLaunched_ = false;

                  @Override
                  public void onGDEvent(GDAppEvent event) {
                    if (event.getEventType() == GDAppEventType.GDAppEventAuthorized
                        && !uiLaunched_) {
                      startActivity(new Intent(LaunchActivity.this, MainActivity.class));
                      uiLaunched_ = true;
                    }
                  }
                });

        authorizeCalled_ = true;
      } catch (GDError gderror) {
        showErrorPopup(gderror.getMessage());
      }
    } else {
      finish();
    }
  }
 @Override
 protected void onResume() {
   super.onResume();
   if (isAppAuthorized_) {
     loadLastOpenedFile();
   }
   resumed_ = true;
 }
 @Override
 protected void onPause() {
   super.onPause();
   if (isAppAuthorized_) {
     saveLastOpenedFileToDisk();
   }
   resumed_ = false;
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ac_text_editor_main);

    saveButton_ = (Button) findViewById(R.id.save_button);
    cancelButton_ = (Button) findViewById(R.id.cancel_button);
    fileContent_ = (EditText) findViewById(R.id.file_content);

    TextEditorGDServiceListener gdServiceListener = TextEditorGDServiceListener.getInstance(this);
    gdServiceListener.setOnOpenFileToEditListener(
        new OnOpenFileToEditListener() {
          @Override
          public void onFileToEditReceived() {
            runOnUiThread(
                new Runnable() {
                  @Override
                  public void run() {
                    if (resumed_ && isAppAuthorized_) {
                      loadLastOpenedFile();
                    }
                  }
                });
          }
        });

    if (!isEditFileServiceWasSetUp_) {
      try {
        GDService.setServiceListener(gdServiceListener);
      } catch (GDServiceException e) {
        Log.e(
            TextEditorMainActivity.TAG, "Error while initializing GDService: " + e.getMessage(), e);
      }
      isEditFileServiceWasSetUp_ = true;
    }

    // Block interface until file will be opened from another app
    saveButton_.setEnabled(false);
    cancelButton_.setEnabled(false);
    fileContent_.setEnabled(false);

    GDAndroid.getInstance()
        .authorize(
            new GDAppEventListener() {
              @Override
              public void onGDEvent(GDAppEvent event) {
                if (event.getEventType() == GDAppEventType.GDAppEventAuthorized) {
                  isAppAuthorized_ = true;
                  loadLastOpenedFile();
                }
              }
            });
  }