简体   繁体   中英

show and hide dialog in android studio

I am having a situation that my dialog doesn't show up in my asyncTask. The codes below are my asyncTask

private class AsyncCallListWS extends AsyncTask<Void, Void, Void> {
@Override
    protected void onPreExecute() {
        Log.i(TAG, "--------------------------------------------------");
        Log.i(TAG, "pending ws: onPreExecute");
        showLoadingDialog();
    }

    @Override
    protected Void doInBackground(Void... params) {
        Log.i(TAG, "pending ws: doInBackground");
        //listDataParent = new ArrayList<Tn_Parent>();
        listPending();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        Log.i(TAG, "Call pending ws: onPostExecute");
        dismissLoadingDialog();

        //Log.i(TAG, "I am not up there "+status.toString());
        if(getContext()!=null) {
            //adapter = new Tn_ListViewAdapter(getActivity(), newList, selectAll);
            //listView.setAdapter(adapter);

            lvAdapter = new Tn_ListViewAdapter(getActivity(), lvList, selectAll);
            listView.setAdapter(lvAdapter);
            // .............. below is not needed .....................
            //listAdapter = new Tn_ExpandableAdapter(listDataParent,getContext(), selectAll);
            //expListView.setAdapter(listAdapter);
        }
    }
}

And below is my dialogbox codes. The dialog work well in other class.

public void showLoadingDialog() {

    if (bar == null) {
        bar = new ProgressDialog(getActivity());
        bar.setMessage(getString(R.string.loading_message));
        //bar.setCanceledOnTouchOutside(getRetainInstance());
        bar.setCanceledOnTouchOutside(false);
    }
    bar.show();
}

public void dismissLoadingDialog() {

    if (bar != null && bar.isShowing()) {
        bar.dismiss();
    }
}

I really wish to know what are the problems. The dialog show up when I put showLoadingDialog() in the onCreateView(), but the problem is that the dialog will not dismiss if i put it inside the onCreateView(). Please help.

For your dismiss() problem inside onCreate , try to change

public void dismissLoadingDialog() {
  if (bar != null && bar.isShowing()) {
      bar.dismiss();
  }
}

to

public void dismissLoadingDialog() {
  if (bar != null) {
      bar.dismiss();
      bar = null;
  }
}

The problem to not showing up your dialog inside AsynTask might be your if (bar == null) { condition, because at that time your bar object will not be null. So that the time when you are dismissing the dialog you have to initialize it to null . And please write bar.show(); this line of code inside if(...) condition.

As Preetika Kaur suggested you should pass a Context object to you showLoadingDialog() and call bar = new ProgressDialog(yourContextObject); cause otherwise the bar would always be null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM