ChrisMcJava
ChrisMcJava

Reputation: 2293

getting previously called activity/intent

I am trying to build an mp3 player in android. I have a MainActivity with 3 Buttons which are as follows: Now Playing, My Songs, and Settings. Each button has an onClickListener. Everything works perfectly with one exception: When I click on a song from the MySongs ListActivity, the Player is loaded and the song starts. When I go back to the main menu via the back button on the android device, then click on MySongs again, and pick a new song, a new Player is loaded and the old one continues to play, so now two songs are playing instead of just the new one. I think the problem is with the way I am using the Intents.

MainActiviy class (I've omitted code that is irrelevant to this problem)

public class MainActivity extends Activity {
    Player player = new Player();;

    @Override
    public void onCreate(Bundle sIS){
        super.onCreate(sIS);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.main);
    }

    ...
    public void launchMySongs(View v) {
        startActivity(new Intent(this, player.getClass()));
    }
    ...
}

and now the Player class (again, I've omitted some stuff I don't think is important)

public class Player extends Activity implements OnCompletionListener, OnSeekBarChangeListener{
    private MediaPlayer mp;
    private Handler mHandler = new Handler();
    private SongsManager sm;
    private int currentSongIndex = 0;
    private boolean isPlaying = false;
    private boolean isShuffle = false;
    private boolean isRepeat = false;
    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    @Override
    public void onCreate(Bundle sIS){
        super.onCreate(sIS);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.player);
        mp = new MediaPlayer();
        sm = new SongsManager();
        mp.setOnCompletionListener(this);
        songsList = sm.getSongList();
        startActivityForResult(new Intent(getApplicationContext(), SongsList.class), 100);
    }

    ...
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 100){
            currentSongIndex = data.getExtras().getInt(SongsManager.INDEX);
            playSong(currentSongIndex);
        }
    };
    ...

and lastly, the SongList class

public class SongsList extends ListActivity {
    public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    private String[] songTitles;
    MainActivity main;

    public void onCreate(Bundle sIS){
        super.onCreate(sIS);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.songslist);
        SongsManager sm = new SongsManager();
        this.songsList = sm.getSongList();
        this.songTitles = new String[songsList.size()];
        for (int i = 0; i < songsList.size(); i += 1){
            try {
                songTitles[i] = (String)songsList.get(i).get(SongsManager.KEY_TITLE);
            } catch (Exception e){
                Log.d("SongsList", "Class Cast Exception");
            }
        }
        this.setListAdapter(new SongListAdapter());
        ListView lv = this.getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                int songIndex = position;
                Intent i = new Intent(getApplicationContext(), Player.class);
                i.putExtra(SongsManager.INDEX, songIndex);
                setResult(100, i);
                finish();
            }
        });
    }
...

any insights would be greatly appreciated. I'm trying to do something along the lines of: Player.getIntent() or something like that instead of calling a new Intent.

Upvotes: 0

Views: 84

Answers (1)

G. Blake Meike
G. Blake Meike

Reputation: 6715

Yeah, the problem is that there is no way that you can be assured that the Activity that you see now is the same instance of Activity (Player, in this case) that you saw earlier. It is definitely not the instance that you create in MainActivity. You should not be creating instances of Activity.

You are going to have to figure out (welcome to Android) how to keep your entire state in either the intent that you use to start a new Activity, or in the Bundle that you are calling sIS.

Have a look at the Service class. I think that you are going to want to make your Player a Service, not an activity. Think web server. An Activity is a servlet, the UI. Middleware should live in a Service.

Upvotes: 2

Related Questions