久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<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="qwawimqqmiuu" 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="qwawimqqmiuu" 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的可靠方法?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  這個問題已經出現了好幾次,我已經閱讀了所有的答案,但我還沒有看到一個真正可靠的方法來處理這個問題.在我的解決方案中,我使用從調用 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();
                      }
                  }
                  

                  然后在調用Activity中,我正常實例化對象,通過init傳入任何參數并設置我的監聽器.

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

                  問題出在:當您在對話框打開時旋轉設備并更改方向時,ActivityMyDialogFragment 對象都會重新創建.為了確保輸入值不會搞砸,我將初始化值設置為 static.這對我來說感覺很奇怪,但由于一次只會有一個這樣的對話,我可以接受.問題出在返回值上.原始偵聽器將被調用.這很好,因為該對象仍然存在,但如果需要更新 Activity 上的 UI(存在),它不會被更新,因為 newActivity 實例現在正在控制 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.

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

                  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.

                  優雅地處理這個問題的最佳做法是什么?

                  What is the best practice for handling this gracefully?

                  推薦答案

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

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

                  您創建 DialogFragment 并定義 Activity 將實現的接口,就像您在上面所做的那樣:

                  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中,當你想將結果返回給Activity時,你將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 中實現該接口并獲取值:

                  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!");
                      }
                  }
                  

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

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

                  相關文檔推薦

                  Get user#39;s current location using GPS(使用 GPS 獲取用戶的當前位置)
                  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(如何檢測位置提供者?GPS 或網絡提供商)
                  Get current location during app launch(在應用啟動期間獲取當前位置)
                  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="qwawimqqmiuu" id='NkX0P'><tfoot id='NkX0P'></tfoot><dl id='NkX0P'><fieldset id='NkX0P'></fieldset></dl></div>
                            主站蜘蛛池模板: 国产乱码精品1区2区3区 | 色婷婷综合久久久中字幕精品久久 | 国产成人精品一区二区在线 | 91av视频在线 | 国产精品视频久久久久久 | 国产一区影院 | 亚洲欧洲成人av每日更新 | 亚洲精品欧洲 | 精品国产一区二区国模嫣然 | 亚洲 欧美 日韩 在线 | 激情毛片 | 色综合久久88色综合天天 | 国产福利91精品一区二区三区 | 中文日韩在线 | 亚洲 中文 欧美 日韩 在线观看 | 91视频在线观看免费 | 久久久精品黄色 | 天天色av | 午夜精品久久 | 日韩精品在线观看一区二区三区 | 羞羞羞视频 | www日韩| 日韩精品一区二区三区在线观看 | 在线欧美小视频 | 日韩不卡一区二区 | 欧美亚洲高清 | 国产精品美女一区二区 | 乳色吐息在线观看 | 久久久久国产精品午夜一区 | 中文字幕精品视频 | 嫩草伊人| 国产精品久久久久久久久免费相片 | 精品三区 | 91精品一区二区三区久久久久久 | 超碰一区二区 | 欧美v日韩v| 日韩高清中文字幕 | 久久久成 | 91久久国产综合久久 | 日韩在线一区二区三区 | 国产福利视频 |