Dismiss non-cancelable dialog on fragment backPress - Android
up vote
6
down vote
favorite
I have a Navigation drawer activity and many fragments that I reach via the nav drawer.
In some of these fragments, I show a dialog which says "Loading.." while background tasks are happening.
Now I've made my dialogs not-cancellable by dialog.setCancelable(false)
so that the user doesn't accidentally dismiss it by clicking anywhere on the screen. This makes it un-cancelable even when the phone back button is pressed.
This is the code for my dialog -
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_progress_dialog);
((TextView)dialog.findViewById(R.id.custom_dialog_message)).setText("Loading ...");
dialog.setCancelable(false);
dialog.show();
I need to write code to dismiss the loading dialog and go to the previous fragment when the phone back button is pressed while on any fragment.
Can somebody help me out? Mostly I need implementing backPress specific to a fragment. Thanks!
android dialog onbackpressed
add a comment |
up vote
6
down vote
favorite
I have a Navigation drawer activity and many fragments that I reach via the nav drawer.
In some of these fragments, I show a dialog which says "Loading.." while background tasks are happening.
Now I've made my dialogs not-cancellable by dialog.setCancelable(false)
so that the user doesn't accidentally dismiss it by clicking anywhere on the screen. This makes it un-cancelable even when the phone back button is pressed.
This is the code for my dialog -
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_progress_dialog);
((TextView)dialog.findViewById(R.id.custom_dialog_message)).setText("Loading ...");
dialog.setCancelable(false);
dialog.show();
I need to write code to dismiss the loading dialog and go to the previous fragment when the phone back button is pressed while on any fragment.
Can somebody help me out? Mostly I need implementing backPress specific to a fragment. Thanks!
android dialog onbackpressed
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I have a Navigation drawer activity and many fragments that I reach via the nav drawer.
In some of these fragments, I show a dialog which says "Loading.." while background tasks are happening.
Now I've made my dialogs not-cancellable by dialog.setCancelable(false)
so that the user doesn't accidentally dismiss it by clicking anywhere on the screen. This makes it un-cancelable even when the phone back button is pressed.
This is the code for my dialog -
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_progress_dialog);
((TextView)dialog.findViewById(R.id.custom_dialog_message)).setText("Loading ...");
dialog.setCancelable(false);
dialog.show();
I need to write code to dismiss the loading dialog and go to the previous fragment when the phone back button is pressed while on any fragment.
Can somebody help me out? Mostly I need implementing backPress specific to a fragment. Thanks!
android dialog onbackpressed
I have a Navigation drawer activity and many fragments that I reach via the nav drawer.
In some of these fragments, I show a dialog which says "Loading.." while background tasks are happening.
Now I've made my dialogs not-cancellable by dialog.setCancelable(false)
so that the user doesn't accidentally dismiss it by clicking anywhere on the screen. This makes it un-cancelable even when the phone back button is pressed.
This is the code for my dialog -
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_progress_dialog);
((TextView)dialog.findViewById(R.id.custom_dialog_message)).setText("Loading ...");
dialog.setCancelable(false);
dialog.show();
I need to write code to dismiss the loading dialog and go to the previous fragment when the phone back button is pressed while on any fragment.
Can somebody help me out? Mostly I need implementing backPress specific to a fragment. Thanks!
android dialog onbackpressed
android dialog onbackpressed
asked Sep 30 '15 at 10:17
Mallika
4151825
4151825
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
7
down vote
accepted
you can use getFragmentManager().popBackStackImmediate();
dialog.setOnKeyListener(new Dialog.OnKeyListener()
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
dialog.dismiss();
getFragmentManager().popBackStackImmediate();
return true;
);
Thanks for the addition to go back to the previous fragment. Worked well!
– Mallika
Sep 30 '15 at 10:40
my pleasure....
– Usman Kurd
Sep 30 '15 at 10:41
add a comment |
up vote
7
down vote
It's simple as this
dialog.setOnKeyListener(new Dialog.OnKeyListener()
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
dialog.dismiss();
return true;
);
add a comment |
up vote
6
down vote
Remove your code line :
dialog.setCancelable(false);
and put this one and try
dialog.setCanceledOnTouchOutside(false);
With this your dialog will not cancel if user accidentally touch on the screen, but if user press back button then it will cancel
add a comment |
up vote
0
down vote
please put the following code where you are initializing the alertDialogbuilder
.
alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setOnKeyListener(new Dialog.OnKeyListener()
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
dialog.dismiss();
getFragmentManager().popBackStackImmediate();
return true;
);
New contributor
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
you can use getFragmentManager().popBackStackImmediate();
dialog.setOnKeyListener(new Dialog.OnKeyListener()
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
dialog.dismiss();
getFragmentManager().popBackStackImmediate();
return true;
);
Thanks for the addition to go back to the previous fragment. Worked well!
– Mallika
Sep 30 '15 at 10:40
my pleasure....
– Usman Kurd
Sep 30 '15 at 10:41
add a comment |
up vote
7
down vote
accepted
you can use getFragmentManager().popBackStackImmediate();
dialog.setOnKeyListener(new Dialog.OnKeyListener()
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
dialog.dismiss();
getFragmentManager().popBackStackImmediate();
return true;
);
Thanks for the addition to go back to the previous fragment. Worked well!
– Mallika
Sep 30 '15 at 10:40
my pleasure....
– Usman Kurd
Sep 30 '15 at 10:41
add a comment |
up vote
7
down vote
accepted
up vote
7
down vote
accepted
you can use getFragmentManager().popBackStackImmediate();
dialog.setOnKeyListener(new Dialog.OnKeyListener()
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
dialog.dismiss();
getFragmentManager().popBackStackImmediate();
return true;
);
you can use getFragmentManager().popBackStackImmediate();
dialog.setOnKeyListener(new Dialog.OnKeyListener()
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
dialog.dismiss();
getFragmentManager().popBackStackImmediate();
return true;
);
answered Sep 30 '15 at 10:28
Usman Kurd
6,49664376
6,49664376
Thanks for the addition to go back to the previous fragment. Worked well!
– Mallika
Sep 30 '15 at 10:40
my pleasure....
– Usman Kurd
Sep 30 '15 at 10:41
add a comment |
Thanks for the addition to go back to the previous fragment. Worked well!
– Mallika
Sep 30 '15 at 10:40
my pleasure....
– Usman Kurd
Sep 30 '15 at 10:41
Thanks for the addition to go back to the previous fragment. Worked well!
– Mallika
Sep 30 '15 at 10:40
Thanks for the addition to go back to the previous fragment. Worked well!
– Mallika
Sep 30 '15 at 10:40
my pleasure....
– Usman Kurd
Sep 30 '15 at 10:41
my pleasure....
– Usman Kurd
Sep 30 '15 at 10:41
add a comment |
up vote
7
down vote
It's simple as this
dialog.setOnKeyListener(new Dialog.OnKeyListener()
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
dialog.dismiss();
return true;
);
add a comment |
up vote
7
down vote
It's simple as this
dialog.setOnKeyListener(new Dialog.OnKeyListener()
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
dialog.dismiss();
return true;
);
add a comment |
up vote
7
down vote
up vote
7
down vote
It's simple as this
dialog.setOnKeyListener(new Dialog.OnKeyListener()
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
dialog.dismiss();
return true;
);
It's simple as this
dialog.setOnKeyListener(new Dialog.OnKeyListener()
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
dialog.dismiss();
return true;
);
answered Sep 30 '15 at 10:19
NaviRamyle
3,19212247
3,19212247
add a comment |
add a comment |
up vote
6
down vote
Remove your code line :
dialog.setCancelable(false);
and put this one and try
dialog.setCanceledOnTouchOutside(false);
With this your dialog will not cancel if user accidentally touch on the screen, but if user press back button then it will cancel
add a comment |
up vote
6
down vote
Remove your code line :
dialog.setCancelable(false);
and put this one and try
dialog.setCanceledOnTouchOutside(false);
With this your dialog will not cancel if user accidentally touch on the screen, but if user press back button then it will cancel
add a comment |
up vote
6
down vote
up vote
6
down vote
Remove your code line :
dialog.setCancelable(false);
and put this one and try
dialog.setCanceledOnTouchOutside(false);
With this your dialog will not cancel if user accidentally touch on the screen, but if user press back button then it will cancel
Remove your code line :
dialog.setCancelable(false);
and put this one and try
dialog.setCanceledOnTouchOutside(false);
With this your dialog will not cancel if user accidentally touch on the screen, but if user press back button then it will cancel
answered Sep 30 '15 at 10:24
warlock
1,5301223
1,5301223
add a comment |
add a comment |
up vote
0
down vote
please put the following code where you are initializing the alertDialogbuilder
.
alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setOnKeyListener(new Dialog.OnKeyListener()
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
dialog.dismiss();
getFragmentManager().popBackStackImmediate();
return true;
);
New contributor
add a comment |
up vote
0
down vote
please put the following code where you are initializing the alertDialogbuilder
.
alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setOnKeyListener(new Dialog.OnKeyListener()
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
dialog.dismiss();
getFragmentManager().popBackStackImmediate();
return true;
);
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
please put the following code where you are initializing the alertDialogbuilder
.
alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setOnKeyListener(new Dialog.OnKeyListener()
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
dialog.dismiss();
getFragmentManager().popBackStackImmediate();
return true;
);
New contributor
please put the following code where you are initializing the alertDialogbuilder
.
alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setOnKeyListener(new Dialog.OnKeyListener()
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
dialog.dismiss();
getFragmentManager().popBackStackImmediate();
return true;
);
New contributor
edited Nov 10 at 15:35
Michal
827919
827919
New contributor
answered Nov 10 at 10:29
francis
1
1
New contributor
New contributor
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32863557%2fdismiss-non-cancelable-dialog-on-fragment-backpress-android%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password