It looks like a bug, but you should rethink showing a dialog from a
background service.

Users reaaaallly don't like pop-up dialogs or other screens suddenly
popping up when they are busy doing something else. The only screen
that can get away with this is the phone-application.

Instead, consider putting a notification in the top status-bar. Then
when the user clicks this notification, start the necessary activity
that is appropriate for your application.


On Jan 11, 7:52 am, Schoel <[email protected]> wrote:
> Hello,
>
> I have found some weird behaviour which might be a bug. I found it
> while developing a quite large application but I've been able to
> reproduce the behaviour in a minimal approach so that I can provide
> source code.
>
> I have a service that is constantly running and at certain times it
> pops up an activity with Theme.Dialog as theme for the user.
> Everything works fine if the main activity of the program has been
> destroyed (via back button) or if the main activity is currently
> running.
>
> However, if the main activity of the app has been created and then
> minimized (e.g. via Home button), the main activity shows up. It is
> not possible to click the buttons of the main activity because it
> seems to be hidden behind an invisible dialog activity. It is possible
> to click the buttons of the dialog if you know where they are and if
> you click the back button, you can see the dialog flashing by as it
> disappears and it is then possible to click the main activity buttons.
>
> I've tested this on HTC Desire 2.2, HTC Wildfire 2.1, HTC Legend 2.1,
> Sony x10 mini pro 2.1 and on emulators 2.1 and 2.2. The behaviour
> described happens on all 2.1 devices and emulator but not on emulator
> 2.2 nor on the Desire with 2.2.
>
> Source code is provided below. There are 2 buttons in the main
> activity, the first, called "Finish" asks the service to popup the
> dialog in 3 seconds and then finishes the main activity. This button
> is for illustrative purposes only, it works fine. The second button,
> called "Post later" just asks the service to popup the dialog in 3
> seconds. If the second button is clicked and before the 3 seconds have
> passed, the home button is clicked, the bug appears.
>
> Source code:
> Main.java:
> --------------------------------------------------------------
> package com.myapp.DialogBugTest;
>
> import android.app.Activity;
> import android.content.Intent;
> import android.os.Bundle;
> import android.os.Handler;
> import android.view.View;
>
> public class Main extends Activity
> {
>     private Handler handler = new Handler();
>
>     private final Runnable runnable = new Runnable()
>     {
>         public void run()
>         {
>             DialogBugTestService.getInstance().showDialog();
>         }
>     };
>
>     /** Called when the activity is first created. */
>     @Override
>     public void onCreate(Bundle savedInstanceState)
>     {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.main);
>
>         startService(new Intent(this, DialogBugTestService.class));
>     }
>
>     public void onFinishButton(View aView)
>     {
>         handler.postDelayed(runnable, 3000);
>         finish();
>     }
>
>     public void onLaterButton(View aView)
>     {
>         handler.postDelayed(runnable, 3000);
>     }}
>
> --------------------------------------------------------------
> main.xml:
> --------------------------------------------------------------
> <?xml version="1.0" encoding="utf-8"?>
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
> android"
>     android:orientation="vertical"
>     android:layout_width="fill_parent"
>     android:layout_height="fill_parent"
>     >
>         <Button
>                 android:id="@+id/finish_button"
>             android:layout_width="fill_parent"
>             android:layout_height="wrap_content"
>             android:text="Finish"
>             android:onClick="onFinishButton"
>             />
>         <Button
>                 android:id="@+id/later_button"
>             android:layout_width="fill_parent"
>             android:layout_height="wrap_content"
>             android:text="Post later"
>             android:onClick="onLaterButton"
>             />
> </LinearLayout>
> --------------------------------------------------------------
> DialogBugTestService.java:
> --------------------------------------------------------------
> package com.myapp.DialogBugTest;
>
> import android.app.Service;
> import android.content.Intent;
> import android.os.IBinder;
> import android.util.Log;
>
> public class DialogBugTestService extends Service
> {
>     private static DialogBugTestService instance = null;
>     @Override
>     public void onCreate()
>     {
>         super.onCreate();
>         instance = this;
>     }
>
>     @Override
>     public int onStartCommand(Intent intent, int flags, int startId)
>     {
>         return START_STICKY;
>     }
>
>     public static DialogBugTestService getInstance()
>     {
>         if (instance == null)
>         {
>             Log.e("service", "instance is null!");
>         }
>         return instance;
>     }
>
>     public void showDialog()
>     {
>         Intent intent = new Intent(this, DialogBugDialog.class);
>         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
>         startActivity(intent);
>     }
>
>     @Override
>     public IBinder onBind(Intent arg0)
>     {
>         return null;
>     }}
>
> --------------------------------------------------------------
> DialogBugDialog.java:
> --------------------------------------------------------------
> package com.optimobile.DialogBugTest;
>
> import android.app.Activity;
> import android.os.Bundle;
> import android.view.View;
>
> public class DialogBugDialog extends Activity
> {
>
>     @Override
>     protected void onCreate(Bundle aSavedInstanceState)
>     {
>         super.onCreate(aSavedInstanceState);
>         setContentView(R.layout.dialog);
>     }
>
>     public void onOk(View aView)
>     {
>         finish();
>     }
>
> }
>
> --------------------------------------------------------------
> dialog.xml:
> --------------------------------------------------------------
> <?xml version="1.0" encoding="utf-8"?>
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
> android"
>     android:orientation="vertical"
>     android:layout_width="fill_parent"
>     android:layout_height="fill_parent"
>     >
>         <Button
>                 android:id="@+id/ok_button"
>             android:layout_width="fill_parent"
>             android:layout_height="fill_parent"
>             android:text="OK"
>             android:onClick="onOk"
>             />
> </LinearLayout>
> --------------------------------------------------------------
>
> I've also tried this without the Theme.Dialog as theme and it then
> works fine. I also tried it with custom styles and the bug seems to
> appear whenever windowIsFloating is set to true.
>
> Is this a known bug? Are there any workarounds? I really need this
> functionality in my app and doing it without a floating window looks
> really ugly.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to