onBackpressed DialogFragment - android

I have 3 custom dialogs (DialogFragment). All are not cancelable, because it is necessary, that the user can't close them. The first dialog starts the second, and the second the third. Now I want to came back to the previous dialog, if I'm using the backclick. At this moment I have two options, but both are not work's really fine:
I start from one dialog the new one, but never call the dismiss. -> So in the background of the next dialog is always the view of the previous dialog
I call dismiss, if I'm start the next dialog, but then he will not return to the previous dialog, but close the dialog.
What can I do, to start the new dialog, so, that the first one is not visible, but if I'm click back, the dialog is visible again?
Thanks a lot for help :))

When you advance from second dialog to third you can close the second one so that you don't see it in the third one.
Later if you need to go back to the second dialog you can re-start it just before closing the third one, the same way you start it when you go from the first dialog to second dialog.
In order to override onBackPressed of a dialog you need to do something like this:
dialog.setOnCancelListener(new DialogInterface.OnCancelListener()
{
#Override
public void onCancel(DialogInterface dialog)
{
// OVERRIDE CODE
}
});
so the work flow would be something like this
firstDialog -> startSecondDialog
firstDialog.dismiss
secondDialog -> startThirdDialog
secondDialog.dismiss
and then if you need to go back you do:
thirdDialog -> startSecondDialog
thirdDialog.dismiss
and so on until you get back to the first dialog.
I hope this makes sense to you, and if not please give us some code so we can help with more details related to your project :)

Related

How to open a new activity without keeping opened the last one?

I'm doing a videogame. I wanted the application to have 3 screens. The presentation screen, the play screen, and the end screen.
I know that an activity can be started with an intent, but my problem is that in doing so, the last activity would be stacked, allowing the user to come back to the previous activity (or screen).
Is there a way to avoid this ?
use the finish() method inside the activity you want to close.
Although others have covered that you can simply call the finish() method to close down an activity if you do not want your user to be able to return to it, there is another issue I wish to cover quickly.
The Android Design Principles, or more specifically the Navigation Principles tell us that we should not be messing around with the default behaviour of the back button too much. A direct quote from the guide;
Consistent navigation is an essential component of the overall user
experience. Few things frustrate users more than basic navigation that
behaves in inconsistent and unexpected ways.
So, instead of preventing your users from being able to return to the entry screen, consider instead a prompt that notifies your user that they will be leaving the game. That way the back button continues to work as they would expect, and your users will not be suddenly dropped from gameplay. You can override the back button like so;
#Override
public void onBackPressed() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Leaving the Game");
alert.setMessage("Do you want to leave the game? You might lose your progress.");
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
YourActivity.this.finish();
}
});
alert.setNegativeButton("Cancel", null );
alert.show();
}
Also, as a note, if you choose to simply close the previous Activity using finish(), the back button will then drop the user out of the app entirely because there is no Activity to go back to.

Dismissing dialog on activity finish

In my app I have several activities one after the other. After my login screen I have Home screen and after that several screens. Now When user select device home button or power off button I want to display login screen when user again comes to my app and then Home screen. Rest all activity I am finishing it from my base class. Now till here I have done, My problem is when I show a dialog in some other activity and at that instance if user click on home or power button, then i am getting WINDOW LEAKED EXCEPTION.
Like I have TempActivity is displaying a dialog and user clicked home button so StoreActivity and TempActivity will finish but Dialog never got chance to be dismissed. So What would be the best way to deal with this situation.
Is there some better way to dismiss the dialog so that I don't get any exception.
Override onDestroy, there, check whether the dialog is present, if so, dismiss it.
dismiss() in onDestroy() doesn't solve this problem. Try to override activity.finish() like:
#Override
public void finish() {
if(mDialog != null) {
mDialog.dismiss();
}
super.finish();
}
Put the Dialog handle in a member object, then when you finish the top activities, dismiss the dialog first.
You could make this more neat by creating abstract Activity class (which all your activities extends), which dismisses possible dialog when calling finish()

android control back button while a dialog is on the screen

I have a timer which runs continously. When I press the BACK button I made a dialog to appear where you can quit from that intent or go back and cointinue the timer what has been stopped by the BACK button. Well if I click on the contimnue, the onResume() method makes the timer continue and it works good. But, if I press the back button when the dialog is on the screen I want the timer to go on just like if I press the Continue on the dialog. But instead, I press the back button and nothing happens, the timer is stopped and it is not good for me since some of my methods only works if the timer is going or it is stopped by the dialog. But if there is no dialog and the timer is stopped numerous potential errors can happen. So how can I stop the user to press the back button when the dialog is on the screen?
I tried something like this:
if ((keycode==back) && a=0 ) {... a=1 , onPuase()} // dialog comes in onPause() just happened
else ((keycode==back) && a=1 ) {... a=0, onResume()} //I want onResume() to happen here
But it is not good. The dialog appears on the first Back button then it disappears on the second Back (nothing happens here). The timer is still stopped here however the third back button starts the timer. So there is an unecessary Back which can cause troubles since the useres wont know that they have to press it again...
A few advices:
Do not call onResume/onPause manually, only system should make it. Else you'll have unexplainable issues on various devices.
You really want to use OnDismissListener ( http://developer.android.com/reference/android/content/DialogInterface.OnDismissListener.html ). As starting from ICS, dialog can be dismissed not only by pressing Back key, but also by tapping somewhere on screen, outside the dialog.
If you want to prevent dismissing the dialog by "back" and "tapping out of dialog" - use setCancellable(false) http://developer.android.com/reference/android/app/Dialog.html#setCancelable(boolean) for the dialog.
Good luck
If you want to be notified when user pressed BACK while your dialog was displayed, use OnDismissListener
implement OnDismissListener in your DialogClass
and override OnDismiss method
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
//you can control back button from here
}

OK Dialog in android

I have an activity that starts on demand of the user.
The user can demand it from several activities.
The thing is I want to give the user an explanation before he has to handle that activity.
I thought about creating a Dialog, giving the user only an OK button to tap on.
But It will be ugly because:
It has to return a value (in my case there is no value I have to return)
The Dialog will have to start the new activity, then when the user presses 'back' button, it will return to the Dialog
Also, if I choose to return to the activity that showed the dialog and start the new activity from there, I'll have to do this in several places (like I explained in the second line of this questions)
Any ideas?
Thanks!
Not sure what you mean by 1. but if you call dismiss() in the onClickListener that handles your OK button, it will not be shown again after you come back from the started activity when you press the back button.
Do make it easier to reuse the dialog, you should create a custom class that handles the dialog. Then you can easily show the dialog from different activities.

Alert user if they hit back arrow

I am looking to alert the user if they hit the back button while in the application.. for instance, if the user is half way through using the application and they hit the back arrow, right now it just closes and they would lose all data if they accidentally hit it.
I would like to be able to alert the user with "Do you really want to exit?" so that if it was accidental, they can choose no and continue, and not lose any data.
I'm guessing I will need to implement some sort of listener??
Override onbackpressed() something like...
#Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
builder.setMessage("Do you really want to exit?.").setCancelable(
false).setPositiveButton("Quit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
YourActivity.this.finish();
}
}).setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
if the user is half way through using the application and they hit the back arrow, right now it just closes and they would lose all data if they accidentally hit it.
Then don't lose the data. Save it in onPause(), if not to the permanent data store, to a temporary spot that you then check sometime later.
I would like to be able to alert the user with "Do you really want to exit?" so that if it was accidental, they can choose no and continue, and not lose any data.
Please don't.
This addresses precisely one use case: the user pressing the BACK button. It completely ignores:
the user pressing the HOME button
the user getting a phone call
the user responding to a Notification
the user long-pressing on HOME (or pressing the recent-tasks button in Honeycomb) and switching to another task
etc.
If losing the data is a problem for you when they press BACK, it is a problem for you in all those other cases as well. Hence, handle all the cases, not by interrupting the user when they are trying to leave, but by holding onto the data, then prompting them about the in-flight data if and when they choose to return.
Just override onBackPressed(). One caveat: it's since API 5.
You should override onBackPressed() method of activity and provide logic there.
There are lots of other threads on this. Basically, override onBackPressed()

Categories

Resources