1

In Android, I am making a simple game. This game is basically comprised of minigames. Each minigame, I have created as an activity. Let's call them ActivityA ActivityB and ActivityC.

I would like to "broadcast" (I use that term very loosely) on completion of any of these activities. When the user beats the minigame, a 1 is "broadcasted", and when they lose, maybe a -1.

I need something to listen for these "broadcasts" and keep track of the score so I know when to end the game.

What's the best way of keeping track of these, where should I start? Personally, I was hoping there would be an invisible activity (or maybe a service) between the main menu, and the game screen, that would do all of the controlling (counting score, telling the app which activities to start, etc).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Josh Beckwith
  • 1,432
  • 3
  • 20
  • 38

3 Answers3

2

You might want to read a little about startActivityForResult.

Quick overview
You can also start another activity and receive a result back.Then your MainActivity receives it in the onActivityResult() callback.

In your "MainActivity" you would have something like this:
You set the intent for the new class and then call startActivityForResult with a unique code to identify the activity that was started)

private void startActivityA() {
    Intent intent = new Intent(this, ActivityA.class);
    startActivityForResult(intent, ACTIVITY_A_CODE);
}

private void startActivityB() {
    Intent intent = new Intent(this, ActivityB.class);
    startActivityForResult(intent, ACTIVITY_B_CODE);
}

private void startActivityC() {
    Intent intent = new Intent(this, ActivityC.class);
    startActivityForResult(intent, ACTIVITY_C_CODE);
}

Then in your MainActivity implement the onActivityResult method, something similar like below in which you will check for what activity was started (*_CODE) and then check whether the result was OK or not.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == ACTIVITY_A_CODE) {
        // if the result of the activity was good, then:
        if (resultCode == RESULT_OK) {
           // score++;
        } else {
           // score--;
        }
    }
    else if (requestCode == ACTIVITY_B_CODE) {
        // ....
    }

}


In your new activities you must set the result, based on your criterias, with setResult()


Please read in more detail about startActivityForResult from the Android Documentation.

  • So basically a dummy activity. This is pretty much how I pictured it going as well. I just wanted to confirm before I spent too much time on it. Thanks for the answer! – Josh Beckwith Nov 01 '16 at 20:39
1

if we think you have MainActivity, ActivityA and ActivityB:

1 In ActivityA you can save result data to db and back to MainActivity and process the result. But check Android Activity lifecycle before https://developer.android.com/training/basics/activity-lifecycle/index.html

2 In android you can return data direcly. Check Android return data to previous activity

In my opinion first way is better. Because it has more opportunities. You can save all kind of data in any situation. (OnClose)

Community
  • 1
  • 1
bmavus
  • 892
  • 1
  • 7
  • 21
0

I think that for your personal requirement there were a variety of alternatives:

  • Use a singleton with the score, you may use livedata , observers, rxjava or other observable solutions to fetch the results.
  • Actually send a Broadcast and receive it where you want it with a BroadCastReceiver
  • Use a Service linked to the application lifecycle.
  • Use a shared ViewModel with MVVM

Sometimes all of those cannot be used because the activity sending the original result is from another app or from the OS. In those cases then the accepted solution can work just fine by delegating the result:

A -> B -> System App -> result to B -> delegate System result to A.
htafoya
  • 18,261
  • 11
  • 80
  • 104