Reputation: 6353
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
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
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