1

I am doing an AsyncTask to call an API call in my application. And showing the API response as an Alert. I have written that API calls on home screen activity and showed that alert on top of the home screen. But if I moved from that activity to another activity, that alert visible once I back to the home screen. Not showing on top of all activities. So my requirement is to show that alert on top of all activities once that API is successful.

Alert code :

 private void AlertSubmitted(String message) {
      
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.alertlayout, null);
        dialogBuilder.setView(dialogView);
        dialogBuilder.setCancelable(false);

        TextView btn_ok = dialogView.findViewById(R.id.btn_ok);
        TextView txt_dia = dialogView.findViewById(R.id.txt_dia);

        txt_dia.setText(message);

        if(subAlertDialog != null && subAlertDialog.isShowing()) {
            return;
        }
        subAlertDialog = dialogBuilder.create();

        subAlertDialog.show();
        btn_ok.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                dbManager.deleteData();
                text.setVisibility(View.GONE);

                subAlertDialog.dismiss();
                return false;
            }
        });

    }

This function is called from protected void onPostExecute(String s).

Malhotra
  • 221
  • 3
  • 13

1 Answers1

0

If you want to make a dialog visible on top of all screens, then try implementing the download code in Service.

After that, You can inflate a custom view to be shown as the dialog layout. Then show the view When the API call is being made and after finishing the API call, show the layout you inflated from your service. It will be canceled when the user taps on the close button or however you want it.

To make a service, try searching Facebook chathead service example android. It will get you some decent examples for creating such service.

Dev4Life
  • 2,328
  • 8
  • 22