Source:
<code>public void onApiChange(SyncthingService.State currentState) { <START> if (mGui == null) { <END> return; } if (currentState != SyncthingService.State.ACTIVE && !isFinishing()) { if (currentState == SyncthingService.State.DISABLED) { if (mLoadingDialog != null) { mLoadingDialog.dismiss(); } mDisabledDialog = SyncthingService.showDisabledDialog(mGui); } else if (mLoadingDialog == null) { final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); LayoutInflater inflater = getLayoutInflater(); View dialogLayout = inflater.inflate(R.layout.loading_dialog, null); TextView loadingText = (TextView) dialogLayout.findViewById(R.id.loading_text); loadingText.setText((getService().isFirstStart()) ? R.string.web_gui_creating_key : R.string.api_loading); mLoadingDialog = new AlertDialog.Builder(mGui) .setCancelable(false) .setView(dialogLayout) .show(); if (prefs.getBoolean("first_start", true)) { new AlertDialog.Builder(mGui) .setTitle(R.string.welcome_title) .setMessage(R.string.welcome_text) .setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { prefs.edit().putBoolean("first_start", false).commit(); } }) .show(); } } return; } if (mLoadingDialog != null) { mLoadingDialog.dismiss(); } if (mDisabledDialog != null) { mDisabledDialog.dismiss(); } mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); mDrawerLayout.setDrawerListener(mDrawerToggle); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); }</code><technical_language>Activity#isDestroyed() do trick ;) (and put next if isFinishing() below)</technical_language>

Target:
public void onApiChange(SyncthingService.State currentState) { if (currentState != SyncthingService.State.ACTIVE && !isFinishing() && !isDestroyed()) { if (currentState == SyncthingService.State.DISABLED) { if (mLoadingDialog != null) { mLoadingDialog.dismiss(); } mDisabledDialog = SyncthingService.showDisabledDialog(this); } else if (mLoadingDialog == null) { final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); LayoutInflater inflater = getLayoutInflater(); View dialogLayout = inflater.inflate(R.layout.loading_dialog, null); TextView loadingText = (TextView) dialogLayout.findViewById(R.id.loading_text); loadingText.setText((getService().isFirstStart()) ? R.string.web_gui_creating_key : R.string.api_loading); mLoadingDialog = new AlertDialog.Builder(this) .setCancelable(false) .setView(dialogLayout) .show(); if (prefs.getBoolean("first_start", true)) { new AlertDialog.Builder(this) .setTitle(R.string.welcome_title) .setMessage(R.string.welcome_text) .setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { prefs.edit().putBoolean("first_start", false).commit(); } }) .show(); } } return; } if (mLoadingDialog != null) { mLoadingDialog.dismiss(); } if (mDisabledDialog != null) { mDisabledDialog.dismiss(); } mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); mDrawerLayout.setDrawerListener(mDrawerToggle); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); }