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

最后都变了- 提交于 2020-04-17 22:45:47

问题


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...


回答1:


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

    });



回答2:


Please use Dialog for creating loaders. the back action will be freeze until the dialog dismiss. the back actions will be work perfectly.

        Dialog dialog = new Dialog(context);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        builder.setTitle(context.getResources().getString(R.string.title))
                .setIcon(R.drawable.icon)
                .setCancelable(false);

        LayoutInflater inflater = LayoutInflater.from(context);
        View dialogView = inflater.inflate(R.layout.dialog_view, null);
        builder.setView(dialogView);
        dialog = builder.create();
        dialog.show();

After api response. dismiss the dialog with the below line of code

       dialog.dismiss();


来源:https://stackoverflow.com/questions/59712970/why-do-i-have-to-click-twice-on-back-button-to-reach-previous-activity-when-prog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!