3

In my app, I require NFC (though, anyone who requires GPS would also run into this). In my MainActivity, I check to make sure NFC is enabled in my onResume method.

When someone pulls down the Notification bar, they could opt to change many settings at the very top including GPS and NFC. I was surprised to learn that the app does not call onPause when the notification menu is pulled down. By disabling NFC through the notification menu, the user is bypassing a layer of my app (at least temporarily).

Is there any way to mitigate this? I suppose I could continuously check if NFC is on once every few seconds, but I think that is overkill.

A very sketchy way of doing it would be to somehow hook into the fact that the screen dims as you pull down, and lightens back up as you push up the notification menu... That might just be my phone though. This is rather cumbersome.

David
  • 7,028
  • 10
  • 48
  • 95

1 Answers1

2

there're broadcasts to receive status changes:

register/unregister to listen for them during your onResume/onPause

Community
  • 1
  • 1
Budius
  • 39,391
  • 16
  • 102
  • 144
  • You said to register and unregister to listen during onResume and onPause. I chose to register in onCreate and unregister in onDestroy, while keeping my onResume/onPause check I currently have. Is there any reason you suggest onResume/onPause over onCreate/onDestroy? – David Jan 11 '15 at 12:09
  • my pleasure. Android is full of Broadcasts for all different kind of information, super useful stuff. – Budius Jan 11 '15 at 12:10
  • Edited my comment, as I had a question. – David Jan 11 '15 at 12:11
  • 1
    answer to extra question: create/destroy the activity can be in background. So I usually do those stuff only when the activity is on-screen. onResume, read latest values, update any `View` and register for updates. onPause, unregister for updates. Some ppl suggest onStart/onStop as well (which won't be called for `Dialogs` showing up). But create/destroy is usually for view creation and config, adapter setup. More view building than data process. – Budius Jan 11 '15 at 12:16
  • Ah, I see. I think I'll go with onStart and onStop. Seems appropriate to me. Again, thanks :) – David Jan 11 '15 at 12:19