Bug 20626 - Alert Dialog closing prior to calling Finish on Intent
Summary: Alert Dialog closing prior to calling Finish on Intent
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 4.10.2
Hardware: PC Mac OS
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2014-06-16 13:39 UTC by Allie Miller
Modified: 2014-06-26 11:11 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

LogicTest test project (14.82 KB, application/zip)
2014-06-16 13:39 UTC, Allie Miller
Async.patch (1.66 KB, application/octet-stream)
2014-06-26 11:11 UTC, Jonathan Pryor

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Allie Miller 2014-06-16 13:39:56 UTC
Created attachment 7082 [details]
LogicTest test project

*Issue reported on behalf of a user*

When combining conditional testing + alert dialogue + finishing action. In the attached example, it doesn't wait for user to make a selection on the Alert Dialog. Instead, it runs with a 'positive' result, immediately. For some reason it is calling finish prematurely in this circumstance. 

Specifically, in the test project, in the following code, IgnoreWarning is the alert dialogue method. Debugging line by line, it refuses to run line 1 properly:
bool Ignore = IgnoreWarning();
if (Ignore == true)
Intent output = new Intent();
output.PutExtra("Selected", "Value");
SetResult(Result.Ok, output);

It jumps the dialogue creation method as soon as it realizes it has to render a dialogue alert. It then just returns the result bool, which in this case is default true. It appears not to render the dialogue until too late. 

To reproduce:
1) Deploy attached LogicTest test project to emulator/device
2) Click the "Hello World, Click Me!" button.
3) Observe the Alert Dialog box appear.

The user should be allowed to select "No" or "Yes"

The Alert Dialog, along with the app, closes with the following output:

>>[WindowManager] Activity logictest.MainActivity has leaked window >>com.android.internal.policy.impl.PhoneWindow$DecorView{416f9fc8 V.E..... R.....I. 0,0-729,390} that was originally added >>here
>>[WindowManager] android.view.WindowLeaked: Activity logictest.MainActivity has leaked window >>com.android.internal.policy.impl.PhoneWindow$DecorView{416f9fc8 V.E..... R.....I. 0,0-729,390} that was originally added >>here
>>[WindowManager] 	at android.view.ViewRootImpl.<init>(ViewRootImpl.java:345)
>>[WindowManager] 	at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
>>[WindowManager] 	at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
>>[WindowManager] 	at android.app.Dialog.show(Dialog.java:281)
>>[WindowManager] 	at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
>>[WindowManager] 	at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
>>[WindowManager] 	at >>mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:29)
>>[WindowManager] 	at android.view.View.performClick(View.java:4240)
>>[WindowManager] 	at android.view.View$PerformClick.run(View.java:17721)
>>[WindowManager] 	at android.os.Handler.handleCallback(Handler.java:730)
>>[WindowManager] 	at android.os.Handler.dispatchMessage(Handler.java:92)
>>[WindowManager] 	at android.os.Looper.loop(Looper.java:137)
>>[WindowManager] 	at android.app.ActivityThread.main(ActivityThread.java:5103)
>>[WindowManager] 	at java.lang.reflect.Method.invokeNative(Native Method)
>>[WindowManager] 	at java.lang.reflect.Method.invoke(Method.java:525)
>>[WindowManager] 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
>>[WindowManager] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
>>[WindowManager] 	at dalvik.system.NativeStart.main(Native Method)

Versioning Information:
=== Xamarin Studio ===
Version 5.0.1 (build 3)
Installation UUID: f6a3c61f-a7e2-4740-ba09-86314760e340
	Mono 3.4.0 ((no/c3fc3ba)
	GTK+ 2.24.23 (Raleigh theme)
	Package version: 304000204
=== Xamarin.Android ===
Version: 4.12.4 (Business Edition)
java version "1.6.0_65"
Comment 1 Ram Chandra 2014-06-16 14:46:20 UTC
I tried this issue and with the help of attached sample I am able to reproduce this issue.

Steps to reproduce:

1) Deploy attached LogicTest test project to emulator/device
2) Click the "Hello World, Click Me!" button.
3) App will be closed.

When I click the "Hello World, Click Me!" I observed that it show the the following message on application output" Activity logictest.MainActivity has leaked window" and close the app. I also debug application line by line and It did not skip any line.  

Screencast: http://www.screencast.com/t/CG4aSJ5L1

Application output : https://gist.github.com/saurabh360/014691676d8a1fe048ff

Environment Info:

Mac OS X 10.9.2

Xamarin Studio : 5.0 (build 878)
 Xamarin.Android : 4.12.4

=== Build Information ===

Release ID: 500000878
Git revision: bcd66920d963483e7d638a2339c4022fe035b529
Build date: 2014-05-27 17:36:26-04
Xamarin addins: da9064ce55b0fa90930a7c437a4cc1ae0e5c778c
Comment 2 Jonathan Pryor 2014-06-26 11:11:00 UTC
Created attachment 7199 [details]

Patch to "asyncify" the app.
Comment 3 Jonathan Pryor 2014-06-26 11:11:20 UTC
Dialog.Show() is NOT a blocking method call.

See also: http://stackoverflow.com/questions/2028697/dialogs-alertdialogs-how-to-block-execution-while-dialog-is-up-net-style

Instead, you need to do one of two things:

1. "Invert" your app logic so that instead of MainActivity.RunLogic() calling this.Finish(), the callback provided to AlertDialog.Builder.SetPositiveButton() does it instead.

2. Use C#5 async/await, as done in Attachment #7199 [details].