I am quite new to android development and I have started making a music player. I have two activities. One is the main that you can see all the albums in the local storage and select one. Upon selecting one a second activity launches that displays the songs from that album. The user can tap on a song and the song plays. I have binded a service to that activity and the song is playing. However when the song starts playing I can not navigate to the previous activity.
I would like to be able to return to the main activity and be able to keep browsing albums and picking another song. I am not sure if I have to bind the service again and how to do it.
Here are some segments of the code.
Manifest file
<activity
android:name=".AlbumActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SongActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="realm.chaos.mplayer.AlbumActivity" />
</activity>
<service android:name="realm.chaos.mplayer.MusicService"/>
And here is the part in the SongActivity that I define and use the musicService.
private ServiceConnection musicConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MusicBinder binder = (MusicBinder)service;
musicSrv = binder.getService();
musicSrv.setList(songList);
musicBound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
musicBound = false;
}
};
@Override
protected void onStart() {
super.onStart();
if(playIntent == null) {
playIntent = new Intent(this, MusicService.class);
bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
}
}
I do not know if that is of help but to play songs I extend the default media controller of Android. I plan to change that in the future since it seems buggy and I am not happy with its functionality. My code is based on this tutorial http://code.tutsplus.com/tutorials/create-a-music-player-on-android-song-playback--mobile-22778. My SongActivity is actually the activity described there and the AlbumActivity is a new activity that I use as parent.
*** Also I have read that you can either bind or start a service. I am not sure which one is suggested. I want to leave the music playing and do change views. Probably I will add more views that might be used to update the service.