pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<i id='Y1Czt'><tr id='Y1Czt'><dt id='Y1Czt'><q id='Y1Czt'><span id='Y1Czt'><b id='Y1Czt'><form id='Y1Czt'><ins id='Y1Czt'></ins><ul id='Y1Czt'></ul><sub id='Y1Czt'></sub></form><legend id='Y1Czt'></legend><bdo id='Y1Czt'><pre id='Y1Czt'><center id='Y1Czt'></center></pre></bdo></b><th id='Y1Czt'></th></span></q></dt></tr></i><div class="n7h7fvj" id='Y1Czt'><tfoot id='Y1Czt'></tfoot><dl id='Y1Czt'><fieldset id='Y1Czt'></fieldset></dl></div>
  • <legend id='Y1Czt'><style id='Y1Czt'><dir id='Y1Czt'><q id='Y1Czt'></q></dir></style></legend>

    <small id='Y1Czt'></small><noframes id='Y1Czt'>

      <bdo id='Y1Czt'></bdo><ul id='Y1Czt'></ul>

      1. <tfoot id='Y1Czt'></tfoot>

        在Android上將值從Dialog傳回Activity的可靠方法?

        Robust way to pass value back from Dialog to Activity on Android?(在Android上將值從Dialog傳回Activity的可靠方法?)
          <legend id='FeAeW'><style id='FeAeW'><dir id='FeAeW'><q id='FeAeW'></q></dir></style></legend>

            1. <tfoot id='FeAeW'></tfoot>
              <i id='FeAeW'><tr id='FeAeW'><dt id='FeAeW'><q id='FeAeW'><span id='FeAeW'><b id='FeAeW'><form id='FeAeW'><ins id='FeAeW'></ins><ul id='FeAeW'></ul><sub id='FeAeW'></sub></form><legend id='FeAeW'></legend><bdo id='FeAeW'><pre id='FeAeW'><center id='FeAeW'></center></pre></bdo></b><th id='FeAeW'></th></span></q></dt></tr></i><div class="377xtvf" id='FeAeW'><tfoot id='FeAeW'></tfoot><dl id='FeAeW'><fieldset id='FeAeW'></fieldset></dl></div>

                <tbody id='FeAeW'></tbody>

              • <bdo id='FeAeW'></bdo><ul id='FeAeW'></ul>

                <small id='FeAeW'></small><noframes id='FeAeW'>

                • 本文介紹了在Android上將值從Dialog傳回Activity的可靠方法?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  這個(gè)問題已經(jīng)出現(xiàn)了好幾次,我已經(jīng)閱讀了所有的答案,但我還沒有看到一個(gè)真正可靠的方法來處理這個(gè)問題.在我的解決方案中,我使用從調(diào)用 ActivityAlertDialog 的偵聽器,如下所示:

                  This question has come up several times and I've read all the answers, but I haven't seen a truly robust way to handle this. In my solution, I am using listeners from the calling Activity to the AlertDialog like so:

                  public class MyDialogFragment extends DialogFragment {
                  
                      public interface MyDialogFragmentListener {
                          public void onReturnValue(String foo);
                      }
                  
                      public void init(boolean someValue)
                      {
                          sSomeValue = someValue;
                          listeners = new ArrayList<MyDialogFragmentListener>();
                      }
                      static boolean sSomeValue;
                      private static ArrayList<MyDialogFragmentListener> listeners;
                  
                      public void addMyDialogFragmentListener(MyDialogFragmentListener l)
                      {
                          listeners.add(l);
                      }
                  
                      public void removeMyDialogFragmentListener(MyDialogFragmentListener l)
                      {
                          listeners.remove(l);
                      }
                  
                      @Override
                      public Dialog onCreateDialog(Bundle savedInstanceState) {
                          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                          builder.setTitle(R.string.title)
                             .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                                 @Override
                                 public void onClick(DialogInterface dialog, int id) {
                                     for (MyDialogFragmentListener listener : listeners) {
                                         listener.onReturnValue("some value");
                                     }
                                 }
                             })
                             .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                                 public void onClick(DialogInterface dialog, int id) {
                                     // User cancelled the dialog
                                     // Nothing to do but exit
                                 }
                             });
                          if (sSomeValue) {
                              builder.setMessage(R.string.some_value_message);
                          } else {
                              builder.setMessage(R.string.not_some_value_message);
                          }
                          // Create the AlertDialog object and return it
                          return builder.create();
                      }
                  }
                  

                  然后在調(diào)用Activity中,我正常實(shí)例化對(duì)象,通過init傳入任何參數(shù)并設(shè)置我的監(jiān)聽器.

                  Then in the calling Activity, I instantiate the object normally, pass in any arguments through init and set my listener.

                  問題出在:當(dāng)您在對(duì)話框打開時(shí)旋轉(zhuǎn)設(shè)備并更改方向時(shí),ActivityMyDialogFragment 對(duì)象都會(huì)重新創(chuàng)建.為了確保輸入值不會(huì)搞砸,我將初始化值設(shè)置為 static.這對(duì)我來說感覺很奇怪,但由于一次只會(huì)有一個(gè)這樣的對(duì)話,我可以接受.問題出在返回值上.原始偵聽器將被調(diào)用.這很好,因?yàn)樵搶?duì)象仍然存在,但如果需要更新 Activity 上的 UI(存在),它不會(huì)被更新,因?yàn)?newActivity 實(shí)例現(xiàn)在正在控制 UI.

                  Here's the problem: when you rotate the device and change orientation while the dialog is open, both the Activity and MyDialogFragment objects get re-created. To ensure that the input values don't get screwed up, I am setting my initialized values as static. This feels hacky to me, but since there will only be one such dialog at a time, I am ok with it. Where the problem comes in is with the return value. The original listener will get called. That's fine because the object still exists, but if there is a requirement to update the UI on the Activity (which there is), it won't get updated because the new Activity instance is now controlling the UI.

                  我正在考慮的一個(gè)解決方案是將對(duì)話框類中的 getActivity() 強(qiáng)制轉(zhuǎn)換為我的 Activity 并強(qiáng)制對(duì)話框本身添加偵聽器,而不是調(diào)用 <代碼>活動(dòng)做.但這感覺就像是黑客的滾雪球.

                  One solution I am considering is casting getActivity() in the dialog class to my Activity and forcing the dialog itself to add a listener, rather than having the calling Activity do it. But this just feels like a snowballing of hacks.

                  優(yōu)雅地處理這個(gè)問題的最佳做法是什么?

                  What is the best practice for handling this gracefully?

                  推薦答案

                  你在正確的軌道上,我按照Android 開發(fā)者 - 使用 DialogFragments 文章.

                  You are on the right track, I follow the method recommended by the Android Developers - Using DialogFragments article.

                  您創(chuàng)建 DialogFragment 并定義 Activity 將實(shí)現(xiàn)的接口,就像您在上面所做的那樣:

                  You create your DialogFragment and define an interface that the Activity will implement, like you have done above with this:

                  public interface MyDialogFragmentListener {
                      public void onReturnValue(String foo);
                  }
                  

                  然后在DialogFragment中,當(dāng)你想將結(jié)果返回給Activity時(shí),你將Activity投射到界面中:

                  Then in the DialogFragment when you want to return the result to the Activity you cast the activity to the interface:

                  @Override
                  public void onClick(DialogInterface dialog, int id) {
                      MyDialogFragmentListener activity = (MyDialogFragmentListener) getActivity();
                      activity.onReturnValue("some value");
                  }
                  

                  然后在 Activity 中實(shí)現(xiàn)該接口并獲取值:

                  Then in the Activity you implement that interface and grab the value:

                  public class MyActivity implements MyDialogFragmentListener {
                      ...
                      @Override
                      public void onReturnValue(String foo) {
                          Log.i("onReturnValue", "Got value " + foo + " back from Dialog!");
                      }
                  }
                  

                  這篇關(guān)于在Android上將值從Dialog傳回Activity的可靠方法?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Get user#39;s current location using GPS(使用 GPS 獲取用戶的當(dāng)前位置)
                  IllegalArgumentException thrown by requestLocationUpdate()(requestLocationUpdate() 拋出的 IllegalArgumentException)
                  How reliable is LocationManager#39;s getLastKnownLocation and how often is it updated?(LocationManager 的 getLastKnownLocation 有多可靠,多久更新一次?)
                  How to detect Location Provider ? GPS or Network Provider(如何檢測(cè)位置提供者?GPS 或網(wǎng)絡(luò)提供商)
                  Get current location during app launch(在應(yīng)用啟動(dòng)期間獲取當(dāng)前位置)
                  locationManager.getLastKnownLocation() return null(locationManager.getLastKnownLocation() 返回 null)
                    • <bdo id='NkX0P'></bdo><ul id='NkX0P'></ul>

                        <small id='NkX0P'></small><noframes id='NkX0P'>

                            <tbody id='NkX0P'></tbody>
                        1. <legend id='NkX0P'><style id='NkX0P'><dir id='NkX0P'><q id='NkX0P'></q></dir></style></legend>
                          <tfoot id='NkX0P'></tfoot>

                            <i id='NkX0P'><tr id='NkX0P'><dt id='NkX0P'><q id='NkX0P'><span id='NkX0P'><b id='NkX0P'><form id='NkX0P'><ins id='NkX0P'></ins><ul id='NkX0P'></ul><sub id='NkX0P'></sub></form><legend id='NkX0P'></legend><bdo id='NkX0P'><pre id='NkX0P'><center id='NkX0P'></center></pre></bdo></b><th id='NkX0P'></th></span></q></dt></tr></i><div class="35z3zfr" id='NkX0P'><tfoot id='NkX0P'></tfoot><dl id='NkX0P'><fieldset id='NkX0P'></fieldset></dl></div>
                            主站蜘蛛池模板: 超高频感应加热设备_高频感应电源厂家_CCD视觉检测设备_振动盘视觉检测设备_深圳雨滴科技-深圳市雨滴科技有限公司 | 乐考网-银行从业_基金从业资格考试_初级/中级会计报名时间_中级经济师 | 好杂志网-首页| 山东螺杆空压机,烟台空压机,烟台开山空压机-烟台开山机电设备有限公司 | STRO|DTRO-STRO反渗透膜(科普)_碟滤 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 合肥网带炉_安徽箱式炉_钟罩炉-合肥品炙装备科技有限公司 | 铝镁锰板厂家_进口钛锌板_铝镁锰波浪板_铝镁锰墙面板_铝镁锰屋面-杭州军晟金属建筑材料 | 中央空调温控器_风机盘管温控器_智能_液晶_三速开关面板-中央空调温控器厂家 | 砂石生产线_石料生产线设备_制砂生产线设备价格_生产厂家-河南中誉鼎力智能装备有限公司 | 粉末冶金-粉末冶金齿轮-粉末冶金零件厂家-东莞市正朗精密金属零件有限公司 | 聚合氯化铝_喷雾聚氯化铝_聚合氯化铝铁厂家_郑州亿升化工有限公司 | 冰晶石|碱性嫩黄闪蒸干燥机-有机垃圾烘干设备-草酸钙盘式干燥机-常州市宝康干燥 | 照相馆预约系统,微信公众号摄影门店系统,影楼管理软件-盟百网络 | 地源热泵一体机,地源热泵厂家-淄博汇能环保设备有限公司 | 排烟防火阀-消防排烟风机-正压送风口-厂家-价格-哪家好-德州鑫港旺通风设备有限公司 | 采暖炉_取暖炉_生物质颗粒锅炉_颗粒壁炉_厂家加盟批发_烟台蓝澳采暖设备有限公司 | 安徽净化工程设计_无尘净化车间工程_合肥净化实验室_安徽创世环境科技有限公司 | SDI车窗夹力测试仪-KEMKRAFT方向盘测试仪-上海爱泽工业设备有限公司 | 等离子空气净化器_医用空气消毒机_空气净化消毒机_中央家用新风系统厂家_利安达官网 | 超细粉碎机|超微气流磨|气流分级机|粉体改性设备|超微粉碎设备-山东埃尔派粉碎机厂家 | 挤塑板-XPS挤塑板-挤塑板设备厂家[襄阳欧格] | 酒吧霸屏软件_酒吧霸屏系统,酒吧微上墙,夜场霸屏软件,酒吧点歌软件,酒吧互动游戏,酒吧大屏幕软件系统下载 | 广东燎了网络科技有限公司官网-网站建设-珠海网络推广-高端营销型外贸网站建设-珠海专业h5建站公司「了了网」 | 高精度电阻回路测试仪-回路直流电阻测试仪-武汉特高压电力科技有限公司 | 低噪声电流前置放大器-SR570电流前置放大器-深圳市嘉士达精密仪器有限公司 | 珠海冷却塔降噪维修_冷却塔改造报价_凉水塔风机维修厂家- 广东康明节能空调有限公司 | 上海办公室装修,办公楼装修设计,办公空间设计,企业展厅设计_写艺装饰公司 | 手机存放柜,超市储物柜,电子储物柜,自动寄存柜,行李寄存柜,自动存包柜,条码存包柜-上海天琪实业有限公司 | 达利园物流科技集团-| 经济师考试_2025中级经济师报名时间_报名入口_考试时间_华课网校经济师培训网站 | 称重传感器,测力传感器,拉压力传感器,压力变送器,扭矩传感器,南京凯基特电气有限公司 | 杭州高温泵_热水泵_高温油泵|昆山奥兰克泵业制造有限公司 | 楼承板-钢筋楼承板-闭口楼承板-无锡优贝斯楼承板厂 | 滑板场地施工_极限运动场地设计_滑板公园建造_盐城天人极限运动场地建设有限公司 | 炉门刀边腹板,焦化设备配件,焦化焦炉设备_沧州瑞创机械制造有限公司 | 数显恒温培养摇床-卧式/台式恒温培养摇床|朗越仪器 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 能量回馈_制动单元_电梯节能_能耗制动_深圳市合兴加能科技有限公司 | 切铝机-数控切割机-型材切割机-铝型材切割机-【昆山邓氏精密机械有限公司】 | 隔爆型防爆端子分线箱_防爆空气开关箱|依客思 |