Android – Why the activity is called twice

androidbroadcastreceiverbuttondialog

So, I'll explain my situation.
I have a Service which is catching the events from the power Button. In the service i have a broadcast receiver which takes Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON.

// BroadcastReceiver for handling ACTION_SCREEN_OFF.
public BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Check action just to be on the safe side.
        if ((intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) ||
                (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) ) {

            if((System.currentTimeMillis() - delta) < DOUBLE_CLICK_INTERVAL) 
            {
                count++;
                Log.e("AAAAAAAA", "count= "+count);
            }else{
                count = 0;
            }

            if(count == 4){
                Log.e("AAAAAAAA", "EVENT TRIGGERED!!!!!");
                Intent alarmIntent = new Intent("com.myaction.action.EVENT_TRIGGERED");
                sendBroadcast(alarmIntent);
            }

            delta = System.currentTimeMillis();
        }
    }
};

I register the receiver in onCreate and i unregister it in onDestroy().
I have another BroadcastReceiver (this is registered in the manifest) which catch my action (EVENT_TRIGGERED). The broadcast receiver launch an activity that wraps a dialog.

public class AlarmBroadReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

    Log.e("BROADCAST", "----------GOT THE EVENT---------");

    Intent newActivityDialog = new Intent(context, NotifyDialog.class);
    newActivityDialog .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(newActivityDialog );

}}

The Activity called by the broadcast is the following:

public class NotifyDialog extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Log.e("DIALOGACTIVITY", "ON CREATE");
    final Window win = getWindow();
    win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                  | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
    win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                  | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);




}



@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    Log.e("DIALOGACTIVITY", "ON START");
    displayAlert();
}





@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Log.e("DIALOGACTIVITY", "ON RESUME");
}





@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    Log.e("DIALOGACTIVITY", "ON PAUSE");
}


@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Log.e("DIALOGACTIVITY", "ON DESTROY");
}





private void displayAlert()
{
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Random question?").setCancelable(
            false).setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //do somethin

                    dialog.cancel();
                    finish();
                }
            }).setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                    finish();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}}

When i push (several times) the power button, my action is broadcast and caught by the proper receiver which launch the activity. My problem is that the activiy is called twice and i dont get the reason, the log gives an idea of what happen:

11-04 16:20:22.339: E/AAAAAAAA(19451): count= 1
11-04 16:20:22.562: E/AAAAAAAA(19451): count= 2
11-04 16:20:22.886: E/AAAAAAAA(19451): count= 3
11-04 16:20:22.983: E/AAAAAAAA(19451): count= 4
11-04 16:20:22.983: E/AAAAAAAA(19451): EVENT TRIGGERED!!!!!
11-04 16:20:23.073: E/AAAAAAAA(19451): count= 5
11-04 16:20:23.077: E/MAMT FESC(19451): ----------GOT SOMETHING---------
11-04 16:20:23.085: E/DIALOGACTIVITY(19451): ON CREATE
11-04 16:20:23.085: E/DIALOGACTIVITY(19451): ON START
11-04 16:20:23.112: E/DIALOGACTIVITY(19451): ON RESUME
11-04 16:20:23.499: E/DIALOGACTIVITY(19451): ON PAUSE
11-04 16:20:23.499: E/DIALOGACTIVITY(19451): ON DESTROY
11-04 16:20:23.554: E/WindowManager(19451): Activity com.notfall.NotifyEmergencyCall has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@482c8228 that was originally added here
11-04 16:20:23.554: E/WindowManager(19451): android.view.WindowLeaked: Activity com.notfall.NotifyEmergencyCall has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@482c8228 that was originally added here
11-04 16:20:23.554: E/WindowManager(19451):     at android.view.ViewRoot.<init>(ViewRoot.java:247)
11-04 16:20:23.554: E/WindowManager(19451):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:181)
11-04 16:20:23.554: E/WindowManager(19451):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:124)
11-04 16:20:23.554: E/WindowManager(19451):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
11-04 16:20:23.554: E/WindowManager(19451):     at android.app.Dialog.show(Dialog.java:241)
11-04 16:20:23.554: E/WindowManager(19451):     at com.notfall.NotifyEmergencyCall.displayAlert(NotifyEmergencyCall.java:115)
11-04 16:20:23.554: E/WindowManager(19451):     at com.notfall.NotifyEmergencyCall.onStart(NotifyEmergencyCall.java:54)
11-04 16:20:23.554: E/WindowManager(19451):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
11-04 16:20:23.554: E/WindowManager(19451):     at android.app.Activity.performStart(Activity.java:3781)
11-04 16:20:23.554: E/WindowManager(19451):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
11-04 16:20:23.554: E/WindowManager(19451):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-04 16:20:23.554: E/WindowManager(19451):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-04 16:20:23.554: E/WindowManager(19451):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-04 16:20:23.554: E/WindowManager(19451):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 16:20:23.554: E/WindowManager(19451):     at android.os.Looper.loop(Looper.java:123)
11-04 16:20:23.554: E/WindowManager(19451):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-04 16:20:23.554: E/WindowManager(19451):     at java.lang.reflect.Method.invokeNative(Native Method)
11-04 16:20:23.554: E/WindowManager(19451):     at java.lang.reflect.Method.invoke(Method.java:521)
11-04 16:20:23.554: E/WindowManager(19451):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-04 16:20:23.554: E/WindowManager(19451):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-04 16:20:23.554: E/WindowManager(19451):     at dalvik.system.NativeStart.main(Native Method)
11-04 16:20:23.554: E/DIALOGACTIVITY(19451): ON CREATE
11-04 16:20:23.558: E/DIALOGACTIVITY(19451): ON START
11-04 16:20:23.620: E/DIALOGACTIVITY(19451): ON RESUME
11-04 16:20:23.636: E/DIALOGACTIVITY(19451): ON PAUSE
11-04 16:20:23.815: E/DIALOGACTIVITY(19451): ON RESUME
11-04 16:20:24.105: E/AAAAAAAA(19451): count= 1
11-04 16:21:19.589: E/DIALOGACTIVITY(19451): ON PAUSE
11-04 16:21:19.667: E/DIALOGACTIVITY(19451): ON DESTROY

My guess is the window leak is caused by the double call to the activity. And as you can see the broadcast is sent and received only once. Sorry for the long mail but i had to explain the whole situation in order to you guys understand why the activity is called twice. Thanks guys.

Daniel

Best Answer

I'm not sure why your activity is getting started twice, but you can remove the leak by clearing up the dialog. Make the dialog a member variable of the Activity, and dismiss and null it onStop if it exists.