I'm a newbie in android and I have a question about that.
How can I make a single application in android? I googled it and I got a method to resolve it is to set launchMode to singleTask or singleInstance, of course I tried but I didn't get a expected resolve.
Actually I had a MP3 player app, when run it then:
Press Home button->press and hold home button-> select my app-> it resumed OK.
But when it is launched then:
Press Back button->press and hold home button-> select my app-> it resumed not OK. I mean android created a new instance of my app, and you know, 2 app run together, but I don't want that.
I tried setting launch mode to singleTask. It works OK in the first activity ( which I set to singleTask) but when I press a button in screen to refer to new activity ( that I dont set to sigleTask) and it didn't work OK.
My app has many activities so i think setting launch mode to singleTask or singleInstane is not good totally.
Looking forward to your answers.
Thank you. @@: I'm very sorry if there any english grammar mistake ( I'm not good at English).
UPDATE:
I have fixd that problem, but now I doubt about my emulator. As you mentioned before, press back button will destroy app, ( call finish() method), so all enviroment that my app holds will be released. But when I pressd back button, my app probably still runs because I still hear the song being played.?
My code:
public class MainActivity extends Activity {
MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = new MediaPlayer();
playSong("sdcard/Music/lung ta lung tung.MP3");
}
public void playSong(String str)
{
try {
mp.setDataSource(str);
mp.prepareAsync();
mp.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
mp.start();
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}