Search code examples
androidprogressdialog

why do I have to click twice on back button to reach previous activity when progress dialog running?


I want when user wants to go back to previous activity while dialog is running ,it should be single click (like youtube).in my case ,on first back press click progress dialog stops and displays a blank page until my data didnt load and on second back press it is then take to previous activity ....

I don't want this twice back press it will be very irritating for user to use app.

is there any solution because I looked other question SO but it didnt matched with my requirement ....

I used this following code for dialog :

    progressDialog = new ProgressDialog(NextActivity.this );
    progressDialog.setMessage("Loading....");
    progressDialog.show();
    progressDialog.setCanceledOnTouchOutside(false);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setCancelable(false);

full code :

 @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nextwhatsandroid_layout);
    progressDialog = new ProgressDialog(NextActivity.this );
    progressDialog.setMessage("Loading....");
    progressDialog.show();
    progressDialog.setCanceledOnTouchOutside(false);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
   // progressDialog.setCancelable(true);

    progressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            //here, dismiss your dialog and finish your activity too
            progressDialog.dismiss();
            finish();
        }
    });

    Toolbar toolbar = (Toolbar) findViewById(R.id. toolbar );
    setSupportActionBar( toolbar );

    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
    Intent intent = getIntent();
    String title = intent.getStringExtra("title");
    String hello=intent.getStringExtra("hii");
    String id = intent.getStringExtra("id");
     Log.e("ashwini", String.valueOf(id));
    getSupportActionBar().setTitle(title);
    /*Create handle for the RetrofitInstance interface*/
    DescriptService service =    DescriptClientInstance.getRetrofitInstance().create(DescriptService.class);

    Call<DescriptionModel> call = service.getAllPhotos(id);

    call.enqueue(new Callback<DescriptionModel>() {
        @Override
        public void onResponse(Call<DescriptionModel> call, Response<DescriptionModel> response) {
            progressDialog.dismiss();
            DescriptList=response.body();
            generateDataList(DescriptList);
        }

        @Override
        public void onFailure(Call<DescriptionModel> call, Throwable t) {
            progressDialog.dismiss();

            Toast.makeText(getApplicationContext(), "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
        }
    });
}
private void generateDataList(DescriptionModel photoList) {
    recyclerView = findViewById(R.id.recyclenext);
    LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(manager);
    recyclerView.setHasFixedSize(true);
    adapter = new NextAndroidAdapter(getApplicationContext(),photoList);
    recyclerView.setAdapter(adapter);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if (item.getItemId() ==android.R.id.home) {
        finish();
    }
    return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
    super.onBackPressed();
}

need help ..thanks in advance...


Solution

  • You can try this way

        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setOnKeyListener(new ProgressDialog.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface arg0, int keyCode,
                                 KeyEvent event) {
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    finish();
                    progressDialog.dismiss();
                }
                return true;
            }
    
        });