Search code examples
androidandroid-intentandroid-activityandroid-broadcastonresume

Android onResume not called after onNewIntent


I have a BroadcastReceiver which should launch activity:

@Override
public void onReceive(Context context, Intent intent)
{
    if (wakeLock == null)
    {
        PowerManager pm = (PowerManager) ApplicationScreen.instance.getApplicationContext()
                .getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    }
    if (!wakeLock.isHeld())
    {
        wakeLock.acquire();
    }

    try
    {
            if (ApplicationScreen.instance != null) {
                Intent dialogIntent = new Intent(context, MainScreen.class);
                dialogIntent.addFlags(Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                ApplicationScreen.instance.startActivity(dialogIntent);
            } else {
                Intent dialogIntent = new Intent(context, MainScreen.class);
                dialogIntent.addFlags(Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                context.startActivity(dialogIntent);
            }

    } catch (NullPointerException e)
    {
    }
}

My MainScreen is:

@Override
public void onResume()
{
    Log.e("TAG", "onResume");
    super.onResume();
}

@Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);
    // getIntent() should always return the most recent
    setIntent(intent);
    Log.e("TAG", "onNewIntent");
}

onResume is never called after onNewIntent. I expect that, if device go to sleep, my BroadcastReceiver should wake it up and launch my MainScreen. Also should tell, that MainScreen extends ApplicationScreen. And ApplicationScreen extends Activity.

EDIT:

Device doesn't wake up. Screen stays off.


Solution

  • The only working solution for me is to use the decrepated PowerManager.SCREEN_BRIGHT_WAKE_LOCK when onNewIntent() is called.

    int SCREEN_BRIGHT_WAKE_LOCK = 10;   
    PowerManager lPm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    WakeLock lWl = lPm.newWakeLock(SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP);
    lWl.acquire(1000);  
    

    This will trigger onResume if the Activity was stopped by pressing the power button. Then you should use the recommended WindowManager flags FLAG_TURN_SCREEN_ON and FLAG_KEEP_SCREEN_ON.