gpt4 book ai didi

android - 在 android 中重新加载 Activity 时,alertdialog 消失

转载 作者:行者123 更新时间:2023-11-29 14:39:19 24 4
gpt4 key购买 nike

我必须开发一个 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();
}
}

这里我使用了两个 fragment ...

在第二个 fragment 中有一个警告对话框:

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

最佳答案

设置 android:configChanges="orientation"not encouraged在安卓系统中。您可以先在 fragment 中声明 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();
}

关于android - 在 android 中重新加载 Activity 时,alertdialog 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16580768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com