Check if context is being viewed or in focus - java

I have an Alert Dialog that I am displaying whenever there is a response from the server or an API. It looks like so:
public void showDeveloperDialog(Context context, String responseMessage) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Response");
alertDialog.setMessage(responseMessage);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.setCancelable(false);
alertDialog.show();
}
If the dialog gets called from example Activity A and there is a delay with the response prompting the user goes to another activity (Activity B), the app crashes since the context that called it is not in view.
How can I check to ensure that the dialog only gets shown if the initial context is in view?

You could cast the context to an Activity to check if the Activity is finished or not, and only display if its not finished using isFinishing() method
Activity activity = (Activity) context;
if (!activity.isFinishing()) {
// You can now display the dialog
alertDialog.show();
}

Related

android.view.WindowLeaked error cause of alert dialog

i'm new to android and i have a problem with alert dialog .
there is a method like this :
private void deletSelected(){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("are you sure?");
dialog.setPositiveButton("Yes" , new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
int deletedRow = getContentResolver().delete(
ContentUris.withAppendedId(CONTENT_URI , selectedRow) , null , null);
if (deletedRow == -1){
Toast.makeText(EditorActivity.this , "not deleted!" , Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(EditorActivity.this , "deleted successfully" , Toast.LENGTH_SHORT).show();
}
if (dialog != null){
dialog.dismiss();
}
}
});
dialog.setNegativeButton("no" , new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
if (dialog != null){
dialog.dismiss();
}
}
});
// Create and show the AlertDialog
AlertDialog alertDialog = dialog.create();
alertDialog.show();
}
and a second method like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// User clicked on a menu option in the app bar overflow menu
switch (item.getItemId()) {
// Respond to a click on the "Save" menu option
case R.id.action_save:
save();
finish();
return true;
// Respond to a click on the "Delete" menu option
case R.id.action_delete:
deletSelected();
finish(); // if i remove this line app will work correctly
return true;
}
return super.onOptionsItemSelected(item);
}
my problem is that if i remove finish() in onOptionsItemSelected method my app will run correctly but it remain in current activity which i don't want . i want after user press yes in confirmation dialog selected row in dataBase delete and back to main activity but without this line selected row will delete but app doesn't back to mainActivity .
and if i add finish() the confirmation dialog disappear immediately after clicking delete and app will back to main activity without deleting anything .
and in logical the following error shows:
08-04 21:57:02.630 28064-28064/com.example.android.pets E/WindowManager: android.view.WindowLeaked: Activity com.example.android.pets.EditorActivity has leaked window android.widget.PopupWindow$PopupDecorView{652e345 V.E...... ......ID 0,0-588,144} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:394)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:330)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1279)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1118)
at android.support.v7.widget.AppCompatPopupWindow.showAsDropDown(AppCompatPopupWindow.java:105)
at android.support.v4.widget.PopupWindowCompatKitKat.showAsDropDown(PopupWindowCompatKitKat.java:33)
at android.support.v4.widget.PopupWindowCompat$KitKatPopupWindowImpl.showAsDropDown(PopupWindowCompat.java:129)
at android.support.v4.widget.PopupWindowCompat.showAsDropDown(PopupWindowCompat.java:206)
at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:721)
at android.support.v7.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:160)
at android.support.v7.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:187)
at android.support.v7.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:290)
at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:175)
at android.support.v7.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:803)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:682)
08-04 21:57:02.631 28064-28064/com.example.android.pets E/WindowManager: android.view.WindowLeaked: Activity com.example.android.pets.EditorAc
any idea ?
EDIT:
after removing finish() and place it in onClick method . everything is correct now . but i can't understand what is the difference between this two way ?
Try passing the activity that you want to finish into the deletSelected() method and call the activity.finish() from onClick(), instead of calling finish() from where you currently have.
This is happening because after the dialog appears when you click on optionmenuitemselected you are killing the activity without dismissing the AlertDialog. Hence the user is not able to select or press "yes" or "no" as he wish.
all you need to do is put onBackPressed() in the alert dialog's positive button or negative if you want to.
private void deletSelected(){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("are you sure?");
dialog.setPositiveButton("Yes" , new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
int deletedRow = getContentResolver().delete(
ContentUris.withAppendedId(CONTENT_URI , selectedRow) , null , null);
if (deletedRow == -1){
Toast.makeText(EditorActivity.this , "not deleted!" , Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(EditorActivity.this , "deleted successfully" , Toast.LENGTH_SHORT).show();
}
if (dialog != null){
dialog.dismiss();
}
**// after performing all you task put this
onBackPressed();
// or if you wish to pass some data to previous activity try onActivityResult way.**
}
});
dialog.setNegativeButton("no" , new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
if (dialog != null){
dialog.dismiss();
}
}
});
// Create and show the AlertDialog
AlertDialog alertDialog = dialog.create();
alertDialog.show();
}
replace new AlertDialog.Builder(this) with
new AlertDialog.Builder(EditorActivity.this); or use getApplicationContext()
On my case, done successfully with this solution:
put "your_activity.this.finish()" inside handler for idle time with dialog.dismiss().
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
your_activity.this.finish();
}
},500);

java Android - two dialogs, prevent first dialog from closing after second one exits

Initially in my app I am creating an AlertDialog which has three buttons, in which the middle button opens up another AlertDialog. The problem is that when the second AlertDialog closes after a button is pressed, the first one closes with it. I think both AlertDialogs get closed after I press a button on the second AlertDialog.
What I want is for the first AlertDialog to open another AlertDialog that has its own buttons, and when second AlertDialog presses a button, it only closes itself and goes back to the first one. Is there any way to achieve this?
Here is the code for the button used to open the AlertDialog:
final ImageButton fabgroup = (ImageButton) findViewById(R.id.groupButton);
Here's the code for a button that opens an AlertDialog that contains another button that opens another AlertDialog using the middle button (create button) on itself, but closes them both when a button on the second one is pressed (either the yes or no button, which is not what I want as I only want the second one to close itself and go back to the first AlertDialog, and yea this sounds pretty confusing in theory so I can try to clarify if needed):
fabgroup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(CreateNote.this);
helpBuilder.setTitle("Select a group");
helpBuilder.setMessage("Add to group?");
final TextView input = new TextView(mainactiv.this);
input.setSingleLine();
input.setText("");
helpBuilder.setView(input);
helpBuilder.setNegativeButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
Toast.makeText(CreateNote.this, "Page has been added to group", Toast.LENGTH_SHORT).show();
}
});
helpBuilder.setNeutralButton("Create", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//open another alertbox
AlertDialog.Builder helpBuilder2 = new AlertDialog.Builder(CreateNote.this);
helpBuilder2.setTitle("Assign a new group");
helpBuilder2.setMessage("Create group?");
final EditText input = new EditText(CreateNote.this);
input.setSingleLine();
input.setText("");
helpBuilder2.setView(input);
helpBuilder2.setNegativeButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Create Group
Toast.makeText(CreateNote.this, "Group has been created", Toast.LENGTH_SHORT).show();
}
});
helpBuilder2.setPositiveButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
// Remember, create doesn't show the dialog
AlertDialog helpDialog2 = helpBuilder2.create();
helpDialog2.show();
}
});
helpBuilder.setPositiveButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
// Remember, create doesn't show the dialog
AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
}
});
Help would be greatly appreciated.
I eventually managed to solve this problem by creating two separate functions to generate each dialog box, and when one closes it calls the function to create the other one, kinda like recycling (or maybe closer to looping functions). Although, I'm not entirely sure how performance heavy this is, but it seems to do the job without any issues from what I'm testing. If anyone would like to chime in on how this could be an issue, then I'm open to hearing what others have to say about the negative points of using alert dialog boxes this way.
You can show an activity as dialog. Put this in your manifest file.
<activity android:theme="#android:style/Theme.Dialog" android:excludeFromRecents="true"/>
From this answer: Android Activity as a dialog

How do I make the AlertDialog box appear outside the app?

#Override
public void run() {
//Create thread that can alter the UI
AlarmPage.this.runOnUiThread(new Runnable() {
public void run() {
cal = Calendar.getInstance();
//See if current time matches set alarm time
if((cal.get(Calendar.HOUR_OF_DAY) == alarmTime.getCurrentHour())
&& (cal.get(Calendar.MINUTE) == alarmTime.getCurrentMinute())){
//If the sound is playing, stop it and rewind
if(sound.isPlaying()){
ShowDialog();
alarmTimer.cancel();
alarmTask.cancel();
alarmTask = new PlaySoundTask();
alarmTimer = new Timer();
alarmTimer.schedule(alarmTask, sound.getDuration(), sound.getDuration());
}
sound.start();
}
}
});
}
public void ShowDialog() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("REMINDER!");
alertDialog.setMessage("Turn off alarm by pressing off");
alertDialog.setNegativeButton("Off", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "OFF", Toast.LENGTH_SHORT);
}
});
alertDialog.show();
}
I am making a simple alarm clock app that notifies the user. I want to make a alert box that gives the user the option to turn off the alarm when it goes off. I was able to make the alert box, but it only appears in the app not outside of the app. I understand the app has to be in the background running. If I need to show more code or be more specific, just ask please.
Add a line as:
public void ShowDialog() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("REMINDER!");
alertDialog.setMessage("Turn off alarm by pressing off");
alertDialog.setNegativeButton("Off", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "OFF", Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
// line you have to add
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
}
check now.
Do not accept answers if they don't address your question, it is misleading.
The accepted answer is not correct, as it will never work outside your application.
Reason:
It requires an activity context not application context.
If you provide application context, your app will crash with IllegalArgumentException- you need to use Theme.AppCompat or their decendents...
If you need functionality as actually stated in the question you have to have a separate activity themed as a Dialog like here
or you can add a custom view to your window using window manager and making it system level alert like here.
Do this create an Activity without ContentView or a View associated with it and call your alertDialog method in your onCreate also remember to set the background of the Activity to Transparent using ColourDrawable
And that activity will look like a dialog or will suit your preference, you can also fall back to Themes so you can set an Activity as Dialog and treat it like Dialog also use DialogFragment

How to show AlertDialog when application is relaunched which previously exited using home button

I am new to Android I am developing an application in API level 2.2.
In my application whenever I press Home button it forces my app to exit. When I relaunch that app by clicking on its icon, it starts from where it exited.
I used CountDownTimer in my application and what i want is showing an Alert Dialog when relaunched.
protected void onResume()
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(StartActivity.this);
alertDialog.setMessage("Resume Current Excersize ?");
alertDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
startCounterTime();//my method
....
}
});
alertDialog.show();
}

Creating a simple yes/no dialog box in a view (Android)

I've got an onCreateDialog method in my activity, which has a case switch to bring up different dialogs that I want to display depending on the request.
I cannot use the showDialog() method from my view because it's not accessible from the context that is passed when the view is created. At least, I can't find a way to access it.
How do I use showDialog from my application's view? Do I need to create a listener? And if so, how? Is there a better method?
Here is my onCreateDialog code that exists in my application's activity:
protected Dialog onCreateDialog(int id) {
AlertDialog alert=null;
switch(id) {
case DIALOG_GAMEOVER_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("You died. Play again?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//init();
//oGameState = eGameState.PLAYING;
dialog.cancel();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
alert = builder.create();
break;
default:
break;
}
return alert;
}
I tried passing a reference to my activity, and I get crashes. Perhaps I am doing it wrong?
In my activity:
// set our MainView as the View
oNebulaMainView = new NebulaMainView(this, this);
setContentView(oNebulaMainView);
In my view:
public NebulaMainView(Context context, Activity oActivity) {
super(context);
// adding the callback (this) to the surface holder to intercept events
getHolder().addCallback(this);
// create the game loop thread
thread = new NebulaMainThread(getHolder(), this);
setFocusable(true);
oActivity.showDialog(DIALOG_GAMEOVER_ID);
}
I may be missing something here, but what is stopping you from just calling:
alert.show()
Just make the alert accessible to the view, and call that form inside your view.
declare alert as an instance variable

Categories