开发者

Android AlertDialog won't show when called in Menu Option

开发者 https://www.devze.com 2023-02-23 06:54 出处:网络
I\'m trying to call an AlertDialog when a certain option in my menu is called. What the user chooses is important to pass as a parameter to an XML page on the net.

I'm trying to call an AlertDialog when a certain option in my menu is called. What the user chooses is important to pass as a parameter to an XML page on the net.

I've tried the following, but my AlertDialog never shows, it goes directly to my next Activity.

@Override
    public boolean onOptionsItemSelected(MenuItem item) {    
        // Handle item selection    
        switch (item.getItemId()) {    
        case R.id.kantoor: 
            //Opent de office activity
            final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
            String arbeiderbed = pref.getString("ArbeiderBediende", "");

            if(arbeiderbed.equals("")){
                final CharSequence[] beroep = {"Arbeider","Bediende"};

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Kies uw statuut");
                builder.setSingleChoiceItems(beroep, -1, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        if (beroep[which].equals("Arbeider")){
                            SharedPreferences.Editor editor = pref.edit();
                            editor.putString("ArbeiderBediende", "arbeider");
                            editor.commit(); // Very important
                        }
                        else{
                            SharedPreferences.Editor editor = pref.edit();
                            editor.putString("ArbeiderBediende", "bediende");
                            editor.commit(); // Very important
                        }                           
                    }
                });
                builder.create().show();

            }
 开发者_如何学C           Intent office = new Intent(MainActivity.this, Settings.class);
            startActivity(office);
            return true;


Of course it does. Alert dialogs are not modal, meaning that the show() method does not block the execution of the further code. If you want the activity to start when the dialog closes, you should put the call to startActivity(), for instance, into the click listener of the dialog button.

0

精彩评论

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