Tim Malseed
Tim Malseed

Reputation: 6353

Android activity binding to service & invoking different commands depending on where the activity was started from.

How do you get an activity to behave differently (invoke different methods), depending on where it was launched from?

I have an activity which is launched either when a user chooses a song, or when a user presses a 'now playing' button.

Ideally, if a song is selected from activity a, then activity b binds to the service, and tells the service to play the selected song.

If the 'now playing' button is selected from activity a, then activity b binds to the service, but doesn't tell it to start playing the song.

I've gathered that this is achievable somehow via either intents, broadcasts, or just a bunch of if statements, but I'm not sure how best to implement this. Thanks for your help.

Upvotes: 1

Views: 123

Answers (2)

Frank Leigh
Frank Leigh

Reputation: 5372

You can add a flag (or any other extra details) to indicate whether you want the song started or not in the intent send to activity B.

So in Activity A:

Intent startBIntent = new Intent(this, ActivityB.class);
Bundle extraDetails = new Bundle();
extraDetails.putBoolean("isPlaySong", true);
startBIntent.putExtras(extraDetails);
startActivity(startBIntent);

Then in onCreate() of Activity B, retrieve the details with:

Boolean isPlaySong = getIntent().getExtras().getBoolean("isPlaySong");

Upvotes: 2

hasanghaforian
hasanghaforian

Reputation: 14022

You can add launcher component name(or other specific string that is unique) as extras to intent that starts Activity.Then in your Activity get it's intent (use getIntent() ) and retrieve it's extras.Then use Switch-Case to decide what would to do.

Upvotes: 2

Related Questions