Carla Urrea Stabile
Carla Urrea Stabile

Reputation: 869

Show dialog and restart an Activity getting LeakedWindow error

I have a language setting in my android app. I save the current language into SharedPreferences and I have a onSharedPreferencesChanged that restarts all activites in the stack so they can be shown in the correct language.

This works like a charm but the language setting is being shown into a Dialog, so when I click on the language I want to change it to, shared preferences changes and then I get a Leaked Window error.

public void showDialog(View v) {
        final CharSequence[] items = { res.getString(R.string.english),
                res.getString(R.string.spanish) };
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(res.getString(R.string.change_language));

        builder.setSingleChoiceItems(items, selected,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int item) {

                        switch (item) {
                        case 0: // English
                            if (selected == 0) {
                                Toast.makeText(
                                        SettingsActivity.this,
                                        res.getString(R.string.current_language),
                                        Toast.LENGTH_LONG).show();
                                break;
                            }
                            changeLocale("en");
                            break;
                        case 1: // Spanish
                            if (selected == 1) {
                                Toast.makeText(
                                        SettingsActivity.this,
                                        res.getString(R.string.current_language),
                                        Toast.LENGTH_LONG).show();
                                break;
                            }
                            changeLocale("es");
                            break;
                        }
                        alert.dismiss();
                    }
                });

        alert = builder.create();
        builder.show();

    }

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    if (key.equals("language")) {
        restartActivity();
    }
}

This method is in the parent activity of all of my activities:

public void restartActivity() {
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}

Im getting a leaked window in the line builder.show() because I'm restarting all the activities when the shared preferences change. How can I show the dialog and restart my activities without getting this error?

Thanks in advance

Upvotes: 1

Views: 1160

Answers (2)

Zsolt Boldizsar
Zsolt Boldizsar

Reputation: 2582

Explaining Abdallah's answer try to move dismiss() up as the first statement in your onClick callback method.

EDIT: First of all after you call alert = builder.create(); you should call alert.show() and not builder.show(). Furthermore having the modifications mentioned in comments you should not receive the error no more.

Upvotes: 1

Abdallah Alaraby
Abdallah Alaraby

Reputation: 2249

Your code is trying to show a Dialog after exiting the Activity.

To prevent this, you should dismiss() your Dialog before restarting Activities.

Upvotes: 0

Related Questions