对话框关闭时工具栏按钮消失

toolbar button disappearing when dialog closed

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

应用预加载 IPaddress="-" 并加载 2 个 asyncTask
一个使用 NsdManager.DiscoveryListener 查找 mDNS 名称并将 IP 加载到 IPaddress 此任务观察 IP 地址 变化并通过 JSON 从设备获取预设并设置 UI 或如果未找到则弹出带有说明的错误对话框.

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

如何让 "Rescan" 按钮保留下来?

.

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

Mike M. 知道了,谢谢 Mike
添加了 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();
     }
 }