user2218667
user2218667

Reputation: 597

alertdialog is disappears while reload the activity in android

I have to develop an one android application.

I have created one alert dialog.If i have to rotate the orientation means that alert dialog is disappears.

But i wish to display the alert dialog when orientation changes.

@Override
   public void onConfigurationChanged ( Configuration newConfig )
  {
      super.onConfigurationChanged(newConfig);
    try
    {
        MainActivity.editCalled = true;
        Intent in = new Intent(AndroidListFragmentActivity.this, AndroidListFragmentActivity.class);
        startActivity(in);
        finish();
    }
     catch (Exception e)
    {
        e.printStackTrace();
    }
}

Here i have using two fragments...

In that 2nd fragment having one alert dialog:

    ImageView share = (ImageView) findViewById(R.id.imageView5);
    share.setOnClickListener(new OnClickListener()
      {
        public void onClick ( View v )
        {
            final CharSequence[] items =
            {
                    "Facebook", "Twitter", "Email"
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(SubCate.this);
            builder.setTitle("Share Via:");
            builder.setItems(items, new DialogInterface.OnClickListener()
            {
                public void onClick ( DialogInterface dialog , int item )
                {
                    if (items[item] == "Facebook")
                    {

                        onFacebookClick();
                    }
                    if(items[item] == "Twitter"){

                        onClickTwitt();
                       } 
                    if (items[item] == "Email")
                    {
                        Intent email = new Intent(Intent.ACTION_SEND);
                        email.setType("message/rfc822");

                        email.putExtra(Intent.EXTRA_EMAIL, new String[]
                        {
                                ""
                        });
                        email.putExtra(Intent.EXTRA_SUBJECT, _Substring);
                        email.putExtra(Intent.EXTRA_TEXT, ContentEmail);
                        startActivity(Intent.createChooser(email, "Choose an Email client :"));

                    }
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
      });
}
Here only i  have facing above problem..please give me solution for these ???

Upvotes: 1

Views: 2866

Answers (2)

Neoh
Neoh

Reputation: 16164

Setting android:configChanges="orientation" is not encouraged in Android. You can first declare the Alertdialog in your fragment and then use onSavedInstanceState:

AlertDialog alert;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.yourid, container, false);

        if(savedInstanceState != null && savedInstanceState.getBoolean("alertShown",true)) {
            showDialog(view.getContext());
        }
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    if(alert != null && alert.isShowing()) {
        // close dialog to prevent leaked window
        alert.dismiss();
        outState.putBoolean("alertShown", true);
    }
}

// put all creating dialog stuff in a single method
protected void showDialog(final Context context) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    ...
    alert = builder.create();
    alert.show();
}

Upvotes: 6

Sagar Maiyad
Sagar Maiyad

Reputation: 12733

As suggested by many people here,

android:configChanges="keyboardHidden|orientation"

is not a solution. It's a hack at best. The correct way to handle this is to manage dialogs through your activity. You need to override a few methods in your activity code, Like so:

protected Dialog onCreateDialog(int id) {
    // create and return your dialog instance here
    AlertDialog dialog = new AlertDialog.Builder(context)
        .setTitle(title)
        .setIcon(R.drawable.indicator_input_error)
        .setMessage(message)
        .create();
    dialog.setButton(
            DialogInterface.BUTTON_POSITIVE,    
            context.getString(R.string.OK),
            (DialogInterface.OnClickListener) null);
    return dialog;
}

protected void onPrepareDialog(int id, Dialog dialog) {
    // You dialog initialization code here
}

After you're done with this. You show your dialog using:

showDialog(yourDialogID);

Once you're done implementing this, your'll see that your dialog will also be recreated if configuration changes occur. The best part is that your Activity will manage your dialog for you. It will be reused when possible, reducing dialog load times if you perform heavy initialization.

Upvotes: 1

Related Questions