Question

I have three activities, each with different layouts..

All three layouts have one thing in common (three ImageButtons) to switch between the activities. In the onClick of the Imagebuttons, I use an intent to open the activity.

Here is the problem: The ActivityA plays a song, now when I click play, the song starts, if I decide to switch to activityB, it opens. From Activity B, if I wanted to go back to ActivityA by clicking the corresponding ImageButton, it creates a new instance of activityA with its default layout (No song is played) but I can still hear the song.

Meaning that a new instance of ActivityA was created, I dont want it to be like this, I want it that when I click the corresponding ImageButton, it goes to the previous activity in the same state as I left it before I switched to ActivityB.

I thought this might solve the problem, but I dont think it would considering I have three Activities (might have used it if it was just two activities)

 public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
 }
Was it helpful?

Solution 2

I got it to work, using

android:launchMode="singleTask" 

in the android manifest.

android:launchMode="singleTop"

didnt work as Mark Bukeima suggested above, but he did push me in the right direction to know I was to use the android:launchMode="singleTask".

OTHER TIPS

I would suggest using a tab navigation or navigation spinner instead of creating new activities.

But if you really want to use separate activities, you could try putting this in the activity tags of your manifest file:

android:launchMode="singleTop"

http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

It might be cause you start your activity using starActivity() instead of startActivityForResult() to switch from ActivityA to ActivityB.

Here's how you should do it :

/* In ActivityA */
public void onClick(View view) {
    Intent intentToB = new Intent(this, ActivityB.class);
    startActivityForResult(intentToB, 0); // Will start B whithout killing A
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top