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

Wini
  • 1,906
  • 1
  • 12
  • 31
  • Can you share image or screen of your trouble in code ? – niceumang Jan 13 '20 at 10:53
  • progressDialog.show(); - why have you added this line twice? – Akanshi Srivastava Jan 13 '20 at 11:37
  • @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 must – Wini Jan 13 '20 at 11:41
  • @Wini I understand the importance of showing the process dialog but why are you calling the show() method twice? – Akanshi Srivastava Jan 13 '20 at 12:00
  • hey I commented one ...not two im showing ...just once im displaying...@AkanshiSrivastava – Wini Jan 13 '20 at 12:02
  • yeah but in your code you're calling the show() method twice – Akanshi Srivastava Jan 14 '20 at 06:48
  • I have removed show() @AkanshiSrivastava do you have solution? – Wini Jan 14 '20 at 07:51
  • @Wini 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://stackoverflow.com/q/28819803/944070 this way you will also solve the problem of double back action press! – madlymad Jan 14 '20 at 19:31

2 Answers2

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

    });
pratik vekariya
  • 1,105
  • 1
  • 8
  • 14
  • where do I call this ?beacuse I put this back pressed() method but still situation remains the same – Wini Jan 13 '20 at 09:46
  • no, write after "progressDialog.show();" and make cancelable true , you can check edited answer. And i think no need of "progressDialog.setCanceledOnTouchOutside(false);" this – pratik vekariya Jan 13 '20 at 09:55
  • this doesnt worked when I click that activity to load..it automatically gets dismiss after 2 sec even if I don't press back button – Wini Jan 13 '20 at 10:13
  • 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? – pratik vekariya Jan 13 '20 at 10:14
  • 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 button – Wini Jan 13 '20 at 10:18
  • 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. – pratik vekariya Jan 13 '20 at 10:49
  • see ..dialog.dismisslistener is called...but why is it being dismiss when activity loaded? – Wini Jan 13 '20 at 14:19
  • This is the correct solution but it should be onBackPressed – cutiko Jan 14 '20 at 14:19
  • @Wini of course dialog will dismiss after activity loaded because your dismissing it when api response is success or failed. – pratik vekariya Jan 15 '20 at 07:24
  • @cutiko when I put this in back pressed still situation remains same.. – Wini Jan 15 '20 at 09:25
  • @pratikvekariya where should I put this then ? – Wini Jan 15 '20 at 09:25
  • @Wini 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. – pratik vekariya Jan 15 '20 at 10:20
  • @pratikvekariya because above of mine ..you are saying it right ? – Wini Jan 15 '20 at 10:35
  • see I have added setondismisslistner at onresponse ,but it goes at infinite loop @pratikvekariya – Wini Jan 15 '20 at 10:39
  • @pratikvekariya just tell me where exactly I should apply setondismisslistner...because current location of setondismisslistner doesnt worked – Wini Jan 15 '20 at 10:48
  • please join [https://chat.stackoverflow.com/rooms/206000/progress-issue] otherwise some can down watt our reputation – pratik vekariya Jan 15 '20 at 11:44
  • @pratikvekariya can you help here-->https://stackoverflow.com/questions/63114801/how-should-i-fetch-images-under-the-array-using-retrofit?noredirect=1#comment111610152_63114801 – Wini Jul 27 '20 at 14:01
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();