views:

264

answers:

1

1- How can i disable multi tasking? My application is a socket based game, every time i start the app, it MUST load the main page first to start the socket connection? I do not want the user to be able to run my application in background. Is this possible to do?

2- I do not want the user to be able to use the back button, to navigate between pages, users must only use the buttons available in my application to navigate? is this possible?

+2  A: 

You can catch the back button (and ignore it), but you cannot block the Home button.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{       
    if (keyCode == KeyEvent.KEYCODE_BACK)
    {
        return true;
    }
    ...

Remember, the Phone is just another application so this OS design prevents a rogue application from disabling the "phone" aspect of the device.

If you want to prevent your application from running in the background, you can close the activity from within the onPause() method:

@Override
protected void onPause()
{
    super.onPause();
    finish();
}

This will force your application to start from scratch if it is put into the background for any reason. This will probably be called when the phone is put to sleep, however, so it might not be the exact behavior you are looking for.

Aaron C
thanks a lot, great answer.1 Question: where do i write the second code u provided? In every activity?
aryaxt
The onPause() code would need to go in any activity that you don't want to allow running in the background. As Chinmay Kanchi commented though, you are bucking against the Android philosophy so it will be very difficult to design your app this way. Here is a more detailed explanation of what lifecycle functions are called when: http://developer.android.com/guide/topics/fundamentals.html#actlife
Aaron C
1 more thing :), i have a service (socket) running in the background. Does calling finish() kill the service as well?
aryaxt
Services are killed by the call stopSelf(). If you started the service by calling startService() it should destroy itself automatically when the activity that started it is destroyed. If you used bindService() you'll have to destroy it manually using stopSelf() to ensure that it is closed.
Aaron C
@Aaron: You have that backwards. If you use startService(), you have to kill it manually. If you start it by binding to it, it will go away when nothing else is bound to it
Falmarri