Tim
Tim

Reputation: 75

Android: Switch between Activities

I am trying to develop an audio player.

My first activity has a list of the mp3's from my sd card and I can choose some them and put them in an ArrayList. I have a button (Add to playlist), and when I press it, it starts the new activity where I take the ArrayList, there I create my player, play music etc. But... when I press the back button on the device(you know this that look like such as a half-circled arrow), I go back to my first activity, but the music don't stop and I can choose again songs and if I press the Add To Playlist button then the app plays two songs simultaneously (my first and this second).

How can I handle this? (I want when i go back, choose or unchoose some tracks and then when i press the button (Add to playlist) to play my new list).

I start my second activity like that:

Intent intent = new Intent (Chooser.this, Player.class);
Bundle b = new Bundle();    
b.putStringArrayList("key", plist);     
b.putStringArrayList("pos", po);           
intent.putExtras(b);
startActivity(intent);  

I don't want my player stops, simply I want to add or remove songs in my playlist.

Upvotes: 1

Views: 166

Answers (2)

Haphazard
Haphazard

Reputation: 10948

You can use a global variable to maintain the "is playing" state. If you find the user tries to play a new song and the isPlaying variable is true then stop the player before starting the new song.

Upvotes: 0

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

You want to implement the one of the Android Lifecycle methods in your second Activity and tell it to stop the MediaPlayer.

The ones you may be interested in are onPause(), onStop(), and onDestroy().

Upvotes: 2

Related Questions