alertdialog正在消失,而重新加载安卓活动 [英] alertdialog is disappears while reload the activity in android

查看:237
本文介绍了alertdialog正在消失,而重新加载安卓活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开发的一款使用Android操作系统的应用程序。

我已经创建了一个警报dialog.If我有旋转方向意味着警报对话框消失。

不过,我想,以显示警告对话框时,方向的变化。

  @覆盖
   公共无效onConfigurationChanged(配置NEWCONFIG)
  {
      super.onConfigurationChanged(NEWCONFIG);
    尝试
    {
        MainActivity.editCalled = TRUE;
        意图=新的意图(AndroidListFragmentActivity.this,AndroidListFragmentActivity.class);
        startActivity(在);
        完();
    }
     赶上(例外五)
    {
        e.printStackTrace();
    }
}
 

在这里,我已经用两个片段...

在有一个警告对话框,第二个片段:

  ImageView的份额=(ImageView的)findViewById(R.id.imageView5);
    share.setOnClickListener(新OnClickListener()
      {
        公共无效的onClick(视图v)
        {
            最后的CharSequence []项目=
            {
                    脸谱,推特,电子邮件
            };

            AlertDialog.Builder建设者=新AlertDialog.Builder(SubCate.this);
            builder.setTitle(「股份通过:);
            builder.setItems(项目,新DialogInterface.OnClickListener()
            {
                公共无效的onClick(DialogInterface对话,诠释项)
                {
                    如果(项目[项目] ==脸谱)
                    {

                        onFacebookClick();
                    }
                    如果(项目[项目] ==推特){

                        onClickTwitt();
                       }
                    如果(项目[项目] ==电子邮件)
                    {
                        意图电子邮件=新的意图(Intent.ACTION_SEND);
                        email.setType(信息/ RFC822);

                        email.putExtra(Intent.EXTRA_EMAIL,新的String []
                        {
                                
                        });
                        email.putExtra(Intent.EXTRA_SUBJECT,_Substring);
                        email.putExtra(Intent.EXTRA_TEXT,ContentEmail);
                        startActivity(Intent.createChooser(电子邮件,选择电子邮件客户端));

                    }
                }
            });
            AlertDialog警报= builder.create();
            alert.show();
        }
      });
}
在这里,只有我已经面对上述problem..please给我的这些解决方案???
 

解决方案

设置安卓configChanges =定向not鼓励的Andr​​oid系统。你可以先声明 Alertdialog 在你的片段,然后用 onSavedInstanceState

  AlertDialog警报;

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){

        查看查看= inflater.inflate(R.layout.yourid,集装箱,假);

        如果(savedInstanceState = NULL和放大器;!&安培; savedInstanceState.getBoolean(alertShown,真)){
            的ShowDialog(view.getContext());
        }
    }
}

@覆盖
公共无效的onSaveInstanceState(包outState){
    super.onSaveInstanceState(outState);

    如果(警报= NULL和放大器;!&安培; alert.isShowing()){
        //为prevent关闭对话框泄露窗口
        alert.dismiss();
        outState.putBoolean(alertShown,真正的);
    }
}

//把所有创建对话框的东西,在一个单一的方法
保护无效的ShowDialog(最终上下文的背景下){
    AlertDialog.Builder建设者=新AlertDialog.Builder(上下文);
    ...
    警报= builder.create();
    alert.show();
}
 

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

解决方案

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

这篇关于alertdialog正在消失,而重新加载安卓活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆