how to trigger UI update when alertdialog gets dismissed - android

I have an AlertDialog which appears initiated by a BroadcastReceiver - so the AlertDialog may appear ontop of ANY of my activities without knowing which activityis actually under it.
private void showAlert(Context context, String s) {
final AlertDialog alertDialog = new AlertDialog.Builder(context)
.create();
alertDialog.setTitle(context.getString(R.string.SMS_Alert_title));
alertDialog.setMessage(s);
alertDialog.setButton(context.getString(R.string.alert_OK),
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return; //can't call the underlying activity's ui update method bec I don't know which activity is actually underlying
}
});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
}
When I now press the "OK" button on the AlertDialog to dismiss it, the pressed status of all my buttons in the underlying activity become unpressed. I need them to remain pressed simply because my buttons show a different png during pressed status - which I also use to show that they are and remain "ON" when pressed. (I can't use toggle buttons in this case)
Ideally I just need to update my UI of the underlying activity but onResume is NOT called when the AlertDialog gets dismissed.
Also I cannot call any UI update method when pressing the ALertDialog OK button, since I do not know which activity is actually under the AlertDialog (as the ALertDialog may appear ontop of any activity)
(I hope I could explain the problem well enough)
ps While I could change the background of the unpressed buttons to the pressed png instead of just saying btn.setPressed(true) I would like to avoid it.
Many thanks

I'm not quite sure what you're trying to achieve. But have a go at following.
Have an interface named... say 'PressableActivity'? which has one method. 'pressAllButtons'
Implement this on all your activities you want the explained functionality, and implement the method to press all buttons when called.
Have a variable of type 'PressableActivity' on your context (or even a static class and a static variable will do.)
Assign this variable to the activity being displayed when it gets a call to onResume.
When you create the dialog, set an onDismissListener to it, which calls the 'pressAllButtons' method on the object pointed by static variable on your context.
Hope this helps.

One way or another you're going to need to "know" which activity is being displayed after the AlertDialog is dismissed. You can set an onDismissListener on your AlertDialog inside of your Activity and then respond accordingly. Why you would want your buttons to remain in a pressed state after they are not pressed is beyond me, but if that's what you really want then just set the state to pressed since that's essentially what you want: a forced pressed state even though the user hasn't pressed it again.

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.

Showing Dialog when Home Screen Causing Gray Screen

I have asynctask running, when finish, I dismiss the progressDialog and call AlertDialog accordingly:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Test")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
Standy on app
Standby on app
Asynctask on going
Asynctask is finished, progressDialog is dismissed
(Still standby on app) AlertDialog appears as it should be
Home screen
Asynctask on going
Go to home screen
Asynctask is finished, progressDialog is dismissed
AlertDialog is called to show
Goes back to app, but alertdialog doesn't appear, it gives me greyed screen instead.
Is there any way to solve this? I tried to search it but couldn't find someone has same issue..
Ok. (As from your comments).
You can not have your dialog automatically appear while your app is in the background. The dialog is displayed within the context of your Activity(along with its window manager), if the activity is not displayed, nor is your dialog.
To display content while your activity is in the background you have a few options, non of which will be straight forward though.
Attach views directly to Android's WindowManager. This will not work with a dialog, you would have to construct a dialog layout and manually attach it, it would remain foreground above everything else.
Launch your dialog from a transparent activity. Problem here is if you are say on your homescreen then the transparent activity/dialog launches and you then call you application the transparent activity/dialog will be dismissed into the background.
Launch an activity with a dialog theme. This "may" work as needed, if you call your main application keeping the stack correct. Would have to test to be sure.
According to the Android Design Guide you should show a Notification instead of the AlertDialog when your AsyncTask has finished and the Activity is in background (paused). To decide what you should show, maintain a boolean variable in your Activity. Set the variable to true inActivity.onResume() and false in Activity.onPaused().
Basically you implement onPostExecute in your AsyncTask. In there you make the ProgressBar invisible and show the Alert Dialog with the code you show in your question.
More about AsyncTask is here
More about Notifications is here
The Design Guide in question is here. Scroll down to 'Sending Notifications to the User'. Essentially, what you do with the AsyncTask in background is like running a Service. At least from the user experience standpoint. Differences between your approach and a Service are internal, e.g. Android will kill your process when it needs space differently.

Calling onResume method after an alert is dismissed in Android?

I want to know if when the user presses "Yes" on an alert dialog and this one is dismissed, that event executes the onResume method of the activity in which the user is in.
Because I have a "Clean" button that asks the user if he's really sure of cleaning all the fields of the form (the activity) in order to redraw the activity with the empty fields.. The form is created dynamically, so I don't know a priori the elements in the GUI to set them empty...
Sorry for my bad english!!
Thanks and Greetings!
Not sure if this is the approach that should be taken, but you should be able to do what I think you are requesting. If for some reason this is what you would want to achieve.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to clean?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.dismiss();
((ActivityName) appContext).onResume();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.dismiss();
}
});
builder.create().show();
}
You will really want to be calling your clean function instead of anything like a lifecycle call on a success while doing nothing on a failure.
Another potential way to approach this would be to bring the current activity back to the front using flags.
Intent intent = new Intent(this, CurrentlyRunningActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Which would also provide a way to call upon your main activity without directly referencing the onResume() call, as pointed out is not the appropriate approach; however, I did want to directly respond to the question as it was asked.
To see if a method is called you can put a breakpoint at the method, onResume(), to see what happens. If you aren't familiar with the Acitvity Lifecycle then doing this will help you familiarize yourself with it and reading the provided documentation.
Now, I don't think you should redraw your whole layout just to clear some Views. It would be more efficient, in my opinion, to just reset all fields by using setText() or another method for whatever you need when the user clicks "ok " or whatever. You can use invalidate() if you need to redraw certain Views
I also recommend watching
Google I/O-Turbo Charge Your UI
Activity LifeCycle <-- very important to understand
AFAIK This is not possible since in order to have displayed the dialog box the activity would have already passed the onResume state. Check out the following page for more on the life cycle on an Android app (it really helped me understand better):
App lifecycle
If you're not sure when the onResume is called, add a log into the onResume method.

Dialog dismisses after screen turned on

I have an application where I have an error dialog pop-up. If i do not touch the screen letting the screen dim off, and then turn the screen on,the pop up dialog is no more there. I want the dialog to be there even if the backlight is turned after it goes off, like in other normal android dialogs.
This is how I am creating the dialog
Dialog lVoiceDialog=new AlertDialog.Builder(this).setIcon(
R.drawable.ic_dialog_alert).setMessage(mVoiceCallMessage)
.setPositiveButton(R.string.yes, clickListener)
.setNegativeButton(R.string.no,clickListener1).create();
lVoiceDialog.setOnDismissListener(dismissListener);
where clickListener and clickListener1 are individual listeners for the positive and negative buttons respectively and dismissListener is listener for running code when the dialog is about to be dismissed.
Please
The other answers make me think I misunderstood the question but just in case..
Your activity is (possibly) being closed when the screen goes off. and restarted when it returns in the normal android activity lifecycle.
To retain your dialog, you would need to save its state when the activity closes and reshow it when it returns.
You can demonstrate this by showing the dialog and rotating the phone. Switching orientation does pretty much the same thing.
use AsyncTask
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
example.dismiss();
}
Why do you need dismissListener?In how many ways your dialog can be closed?if only when user selects 'ok' or cancel then you don't need dismiss listener.
if you have to do some task before the dialog closing then you can do it in ok or cancel listener .
Also, if you for any reason happen to call finish() in onPause() method, getting rid of it can solve the issue.

Android - entries in dialog should be deleted when dialog is closed

I have a customDialog with input fields. I want possible entries to be removed once the dialog is closed (either via back or when a certain button is pressed), i.e. the state should not be saved.
How can I do that?
If the back button is pressed means that dialog is canceled. Implement DialogInterface.OnCancelListener for your dialog and empty/delete/null the entries you want.
I think you might be getting another problem.
Say that you have shown a dialog which was dismissed. If the same dialog is going to be shown a second time, it won't be rebuilt. It will just be shown again.
This means that if you setup your dialog in the onCreateDialog method, the second time that the dialog is shown, this method isn't invoked! Instead, onPrepareDialog is invoked.
Alternatives? You can call Activity.removeDialog or take care of the setup process in the onPrepareDialog hook.

Categories

Resources