1

1 つの Android アプリケーションを開発する必要があります。

アラート ダイアログを 1 つ作成しました。方向を回転させる必要がある場合、アラート ダイアログが消えます。

しかし、向きが変わったときに警告ダイアログを表示したいと思います。

@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();
    }
}

ここでは、2 つのフラグメントを使用しています...

1 つのアラート ダイアログを持つ 2 番目のフラグメントでは、次のようになります。

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

2 に答える 2

6

Androidでは設定android:configChanges="orientation"推奨されません。Alertdialog最初にフラグメントで を宣言してから、次を使用でき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();
}
于 2013-05-16T07:27:48.893 に答える