Paul
Paul

Reputation: 970

How to make a single instance application in android?

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();
    }
}

Upvotes: 1

Views: 1234

Answers (2)

SuperFrog
SuperFrog

Reputation: 7674

Generally the android approach is that the OS is handling the application.

When You press the back button in an Android application, the code may or may not handle that press.

In the scenario you described the back button is probably handled in code and when it pressed the app calls "finish();" on the activity. For example:

@Override
 public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
       //this will finish the activity
       finish(); 
       //this will act as the home button (Use only one of them)
       //moveTaskToBack(true); 
       return true;
    }
 }

When you press the home button, there is an event which the code might handle (onPause). There are scenarios in which you want to terminate the app when someone presses home button and scenarios you want to do something else (e.g. Just move the activity to the background).

As mentioned in the comments, You really should read about the lifecycle of an application Managing the Activity Lifecycle

Upvotes: 0

maninder singh
maninder singh

Reputation: 1286

You can make back button as home by moving task to background as

@Override
public void onBackPressed() {

    moveTaskToBack(true);

}

Upvotes: 2

Related Questions