5

people familier with android (~~ broadcast reciever) , help me out.

I have two activities , main and second. from main , you can go to second if a broadcast is triggered. (eg if flight mode is on, you can go to second activity). I know how to make a reciever for this and the best case here should be that of a dynamic broadcast reciever.

But In the SecondActivity , i again want to implement a similer check, since anyone can turn the flight Mode on and off from the status bar.So if flight mode is on, certain layouts should be shown else not. again a reciever is required

My problem is : is static reciever a better choice, if yes then how can I interact with the reciever, like it should request changes only if either of the following activities are open and application is running..??

(And I don't know why daddy yankee's shaky shaky video is being played in the back of my head)

Comments
  • 0
    use a dynamic reciver, registered when the activities start. when the activities end, unregister the reciver.
    you can set the when switching activities, unregister, and re-register.
    the other option is to put it in the app manifest and select which activity to start accourding to a stateless logic.
  • 1
    Thanks, but should i really use 2 dynamic receivers for 2 activities which are doing exactly the same thing?that sounds logically correct but a lot of repetitive code :|

    The other option is a static one but how will it interact with 2 activities?and how will it know which activity is being run at the first place? Sounds like a complex solution

    @magicMirror
  • 0
    :facepalm:. srsly?
    Use one reciver class, and reference it from both activities. pass parameters to the reciver as required.
    btw, if you have more then one activity in your android app, then you are probably foing something wrong...
  • 1
    Thankyou so much it helped :bigsmile:

    Its a big app, so yeah 2 activities are required.
    Nd sorry, i know it was pretty naive, and i really know my things. its just that this usecase never came when i was first learning this. Plus i am not much of advanced dev. So yeah am still in my developing phase😅

    @magicMirror
  • 0
    @chaostools 😁. no worries.
    Take a look at my rants, there is one there about how I Iearned to use brecivers correctly.
Add Comment