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

2

0

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

Holley answered 13/1, 2020 at 8:33 Comment(8)
Can you share image or screen of your trouble in code ?Redwood
progressDialog.show(); - why have you added this line twice?Flagstone
@AkanshiSrivastava ..if I don't show progress dialog then it will display a blank till my data data loads..it will be inconvenient for user..progrees dialog is mustHolley
@Holley I understand the importance of showing the process dialog but why are you calling the show() method twice?Flagstone
hey I commented one ...not two im showing ...just once im displaying...@AkanshiSrivastavaHolley
yeah but in your code you're calling the show() method twiceFlagstone
I have removed show() @AkanshiSrivastava do you have solution?Holley
@Holley I would suggest to completely remove the ProgressDialog logic is outdated and a bad design pattern. In case you want to visualize the loading you can try this aproach https://mcmap.net/q/541004/-set-progress-bar-on-top-of-recyclerview-and-remove-after-data-is-loaded/944070 this way you will also solve the problem of double back action press!Primitivism
M
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;
        }

    });
Mohamed answered 13/1, 2020 at 8:51 Comment(17)
where do I call this ?beacuse I put this back pressed() method but still situation remains the sameHolley
no, write after "progressDialog.show();" and make cancelable true , you can check edited answer. And i think no need of "progressDialog.setCanceledOnTouchOutside(false);" thisMohamed
this doesnt worked when I click that activity to load..it automatically gets dismiss after 2 sec even if I don't press back buttonHolley
comment your dialog code and copy my edited answer. I have tried and that working. where are you displaying dialog?in onCreate or somewhere else?Mohamed
see full code above...when I click that activity to load..it automatically gets dismiss after 2 sec even if I don't press back buttonHolley
debug app and check whether dialog.dismisslistener called or not? because same code i have tried and its working. remove onBackpress method and also comment api call just for test.Mohamed
see ..dialog.dismisslistener is called...but why is it being dismiss when activity loaded?Holley
This is the correct solution but it should be onBackPressedFriulian
@Holley of course dialog will dismiss after activity loaded because your dismissing it when api response is success or failed.Mohamed
@Friulian when I put this in back pressed still situation remains same..Holley
@pratikvekariya where should I put this then ?Holley
@Holley i think it is at correct place because if don't hide progress after api success or failed then progress will continue showing even if your data is loaded.Mohamed
@pratikvekariya because above of mine ..you are saying it right ?Holley
see I have added setondismisslistner at onresponse ,but it goes at infinite loop @pratikvekariyaHolley
@pratikvekariya just tell me where exactly I should apply setondismisslistner...because current location of setondismisslistner doesnt workedHolley
please join [chat.stackoverflow.com/rooms/206000/progress-issue] otherwise some can down watt our reputationMohamed
@pratikvekariya can you help here-->#63115301Holley
E
0

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();
Expensive answered 13/1, 2020 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.