开发者

TabActivity with ActivityGroup and PreferenceActivity child

开发者 https://www.devze.com 2023-04-12 05:02 出处:网络
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.

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开发者_JS百科);
    }

    @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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号