我必须开发一个Android应用程序。
我创建了一个警告对话框。如果必须旋转方向,则意味着警报对话框消失。
但是我想在方向改变时显示警告对话框。
@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();
}
}
这里我使用了两个片段......
在第二个片段中有一个警告对话框:
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 ???
答案 0 :(得分:5)
在Android中设置android:configChanges="orientation"
为not encouraged。您可以先在片段中声明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();
}
答案 1 :(得分:1)
正如这里很多人所说,
android:configChanges="keyboardHidden|orientation"
不是解决方案。这是一个充其量的黑客。处理此问题的正确方法是通过您的活动管理对话框。您需要覆盖活动代码中的一些方法,如下所示:
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
}
完成此操作后。您可以使用以下方式显示对话框:
showDialog(yourDialogID);
完成此操作后,您将看到如果发生配置更改,还会重新创建对话框。最好的部分是您的Activity将为您管理对话框。它将在可能的情况下重用,如果执行大量初始化,则会减少对话框加载时间。