1

I have a TabActivity where each tab has ActivityGroup. On the home ActivityChild of the first group I have an menu option, which gives to the user the option to open preferences. When I click "Preferences" on menu, I start PreferenceActivity inside ActivityGroup, which shows PreferenceActivity on the first tab. The problem is when I click on any specific preference which has to show a Dialog (for EditTextPreference). I have the following exception:

android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@405d3a20

I understand that the problem is because Dialog to be shown by PreferenceActivity uses wrong context, BUT i don't now how change the context of created dialog.

Belows is the PreferenceActivity I've created.

public class PreferencesActivity extends PreferenceActivity  implements OnSharedPreferenceChangeListener{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preferences);
        addPreferencesFromResource(R.xml.preferences);
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        prefs.registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {     

    }   
}

I don't want to create custom dialogs. I want to use the mechanism of PreferenceActivity for that. Below is the code that I'm using to add to group:

i = new Intent(MyActivity.this, PreferencesActivity.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("PreferencesActivity", i);

Any ideas?

user738686
  • 147
  • 2
  • 12

1 Answers1

0
This is very common problem with dialog's in Tab Host.

Actually the Activity context is not sufficient to show a Dialog in Tab.


You have to use the context of your GroupActivity for the dialog to be enabled without exception
Arpit Garg
  • 8,476
  • 6
  • 34
  • 59
  • Hi, I don't use custom Dialogs, that's why I don't now how pass correct context. Because if I control creation of dialogs I would use getParent() as context. – user738686 Oct 13 '11 at 11:53
  • Just you are having context problem.. In case of Root TabActivity always use context of Tab Activity even for the the Individual Activities in tab – Arpit Garg Oct 13 '11 at 12:03
  • I added more code, maybe you can understand better my problem. – user738686 Oct 13 '11 at 19:07
  • As a workaround I started PreferenceActivity outside of ActivityGroup as a separate activity. Anyway thanks for comments. – user738686 Oct 19 '11 at 18:03