【问题标题】:toolbar button disappearing when dialog closed对话框关闭时工具栏按钮消失
【发布时间】:2019-11-21 21:44:56
【问题描述】:

我的应用程序相当简单,只要我的 IOT 设备启动,它就可以正常工作。
如果找不到设备,我需要加载一个弹出窗口并在工具栏上显示重新扫描按钮。

应用预加载 IPaddress="-" 并加载 2 个异步任务
使用 NsdManager.DiscoveryListener 查找 mDNS 名称并将 IP 加载到 IPaddress 此任务监视 IPaddress 更改并通过 JSON 从设备获取预设值,并设置 UI 或在未找到时弹出带有说明的错误对话框。

我的问题: 当 counter >= 15 时,我使用 setMenuVisible() 在工具栏上显示“重新扫描”按钮,然后弹出错误对话框,但是当按下对话框中的按钮关闭对话框时,“重新扫描”按钮再次消失。
也会在大约 5 秒后超时。

如何让“重新扫描”按钮保持不变?

.

private  class getSettingsFromClock extends AsyncTask<Void, Void, Void> {
  @Override
  protected Void doInBackground(Void... params) {
     String mlooper = IPaddress;
     Log.i(TAG, "LOG getSettingsFromClock doInBackground started ");
     int counter = 0;
     while ( mlooper.equals("-")   ) {
        mlooper = IPaddress;
        try {
            Thread.sleep(600);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        counter++;
        if (counter >= 15)  // in normal operation counter never goes above 3
        {
          Log.i(TAG, "LOG getSettingsFromClock - NO IP Found, count= " + counter );

          runOnUiThread(new Runnable() {
          @Override
          public void run() {

            setMenuVisible( true, R.id.action_rescan);  // show rescan button on toolbar

            try {       // delay is debugging only
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //scanning failed Popup Dialog
            final Dialog dialog = new Dialog(context );
            dialog.setContentView(R.layout.popup);
            dialog.setTitle("Scan Error");

            Button button = dialog.findViewById(R.id.Button01);
            button.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View view) {
                    dialog.dismiss();
               }
            });
            dialog.show();

              Toast.makeText(getApplicationContext(),
                "Could Not get presets from clock. \n check Clock is on and on WiFi\n and reload app.",
              Toast.LENGTH_LONG).show();
           }
          });
               break;
        }
     }
     if( IPaddress != "-" )
     {
       // gets JSON here
     } else
     {
       // add popup - IOT Not found
     }

     // JSON starts here
     if (JSON_return != null) {
            try {
                // loads presets from JSON to UI here
            } catch (final JSONException e) {
                Log.e(TAG, "LOG, JSON parsing error: " + e.getMessage());
            }
     } else 
    {
      Log.e(TAG, "LOG, Could Not get JSON from Clock.");
    }
   return null;
  }
}  // end asyncTask class


 // remember to run on main thread
 // NOTE; private Menu option_Menu; declared in MainActivity
 //  ie;   setMenuVisible( true, R.id.action_rescan);
 public void setMenuVisible(boolean visible, int id) {
   if (option_Menu != null) {
     option_Menu.findItem(id).setVisible(visible);
   }
 }

【问题讨论】:

  • 这不是修改菜单项的非常可靠的方法,因为选项菜单会不时重新创建,并且先前实例的可见性设置将丢失。相反,你可以保留一些标志字段——例如,private boolean showRescan;——然后检查它并在onCreateOptionsMenu()中相应地设置可见性。当您想更改可见性时,根据需要设置标志,然后调用invalidateOptionsMenu()
  • Mike M. - 我不知道菜单会定期重新创建,您的指针已修复
  • 糟糕,我想说的是onPrepareOptionsMenu(),而不是onCreateOptionsMenu()。不过,看起来你知道这一点。真高兴你做到了。干杯!

标签: android dialog toolbar menuitem


【解决方案1】:

迈克 M. 有它,谢谢迈克
添加了 onPrepareOptionsMenu()
showRescan = visible;invalidateOptionsMenu(); 添加到 setMenuVisible()
现在一切正常。

   @Override
   public boolean onPrepareOptionsMenu(Menu menu) {
     super.onPrepareOptionsMenu(menu);
      try {
         if( showRescan )
         {
            option_Menu.findItem(R.id.action_rescan).setVisible( true );
         } else
         {
            option_Menu.findItem(R.id.action_rescan).setVisible( false );
         }
      }
        catch(Exception e) {
            Log.e(TAG, "onPrepareOptionsMenu error");
        }
     return true;
    }

  // when task is completed you can show your menu just by calling this method
  // remember to run on main thread
  //  ie;   setMenuVisible( true, R.id.action_rescan);
  public void setMenuVisible(boolean visible, int id) {
      if (option_Menu != null) {
         option_Menu.findItem(id).setVisible(visible);
         showRescan = visible;
         invalidateOptionsMenu();
     }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 2013-04-06
    • 1970-01-01
    • 2015-02-25
    相关资源
    最近更新 更多