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?
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.
Related
This question already has answers here:
Run code when Android app is closed/sent to background
(8 answers)
Closed 3 years ago.
My android application is a client, and when someone click the home button, I want to send a message to the server, and when the user changes back to the application, I also want to send a message to the server. If I could overwrite the HOME button keyEvent, or any method which will be called only when the application will be put into background, I could send message and set a static variable in a singleton which will be checked every onStart(), so basically I just need to somehow "override" the HOME button.
I tried the followings:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
//...
return true;
}
return super.onKeyDown(keyCode, event);
}
It does simply not work. I put a breakpoint in it, but HOME button doesn't trigger this method at all. (Samsung Galaxy Ace, android 2.2.1).
I also tried to overrite the onUserLeaveHint() like this:
#Override
protected void onUserLeaveHint() {
//...
super.onUserLeaveHint();
}
But the problem is, this method will be called not only when I press HOME button or the application is interrupted, but when I just navigate to an other activity (and finish the current one).
Are there any solution to this problem?
Might want to take a look at this Android Docs
You are looking for onPause / onResume, based on the line
or any method which will be called only when the application will be
put into background
That doc should explain how the stack works, which will allow you to accomplish this problem.
There is not an day solution to this problem, because android does not let you override the home button. The easiest way around this is to pur your code in onPause, but of course this can be called without the home button being pressed.
The second (not accepted) answer here has an interesting solution, but I have not tried it so I am not sure if it works
I know that android does not allow us to catch the home button press; however, I have my own built home screen replacement app and want to just know when the button was pressed to allow animations.
So, my app has just one activity as I do most of my "activities" as canvas drawing so I can have complete control over the visual aesthetics of my app. The problem is if the user navigates to a different page within the app and presses home, nothing happens, since the app is technically already in the same activity.
I want to know when the button is pressed so I can then animate/navigate my app back to the main screen. Also, I know I could accomplish this by having separate real activities however that won't work for what I'm trying to do.
You need to add <category android:name="android.intent.category.HOME"/> to your activity's intent filter in your app's Android.xml
You can take a look at "Home" app in Android SDK samples. They can be downloaded using these instructions.
//overriding method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
//The Code Want to Perform.
}
});
You can detect when the home button is pressed because onPause() is always called. You are not allowed to override the home button, but the following should work
#Override
protected void onPause()
{
super.onPause();
//code here
}
Of course the problem with using this is that it will be called even if the user is not quitting the app(ie. If they are sent to another application through an intent)
That is the closest that you are going to get to a onHomeButtonPressedListener there are really no ways to do this
Home key press Event Listener
In my game, if a user hits the back button I pop up a dialog asking if they really want to quit. However, I can't do the same with the home button because there's no way to override it.
If the user knows the task manager trick they can hold down home and switch back to the app and not lose their place.
If they don't know the trick they'll just select the icon again which will start the application over from the main menu.
Is there any way to override this behavior so that instead of starting at the main menu it would go back to where it was if the app is currently running?
I know that I could save the state of everything when the app pauses and then programmatically reload everything and send them to where they were. I'd like to avoid having to do all that work if possible.
However, I can't do the same with the home button because there's no way to override it.
You can,actually,override the home button.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
//do your stuff
return true;
}
return super.onKeyDown(keyCode, event);
}
Is there any way to override this behavior so that instead of starting at the main menu it would go back to where it was if the app is currently running?
About this, your best bet is to override the onPause() method in every Activity. But there is no guarantee about it, because, the OS might choose to kill your application when the user navigates away from it(for resource requirements,maybe). So in such a case, your application will start off from the main menu, and you can't help it.
I'm starting an activity from a service, which displays some text.
Now if a user presses a key - say a back key, I would like that back button to be processed by any other app which may be running (not started by my service).
Is this possible? I have tried FLAG_NOT_FOCUSABLE, however, the back key is still not processed by any other app/ignored. Android, after some time keep giving a option for "Force Close" or 'Wait".
Any pointers will be helpful.
Just FYI, "whitepages" app does something like this - it shows a dialog window, however, doesn't process the back key.
Thanks.
I'm not sure if this is correct or not, but I've seen the same behavior in a few apps myself. I think they are using Toast messages to display the text. I'm not sure if a service can display Toast or not - you'll have to try... if not then you could also try having the Activity display the toast and immediately exit.
Only the currently running activity receives in the button pressed signal. All you can do is intercept the back button press by implementing:
onBackPressed()
On older devices this won't quite work and you'll need to do something like:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Do something here
return true;
}
return super.onKeyDown(keyCode, event);
}
As far as passing the button press to another activity, I'm not sure you can do that. What you could do is pass the activity an intent that could specify the back button was pressed. However, this assumes that the other activity is setup to deal with such an intent.
I would like to display some images when the application is opened for the first time. Or if its being reopened. I don't want the application to be killed when the user presses the back button, to go to the home screen. But instead keep it alive but still return to the home screen.
As Andro_Selva said, the back button finishes your activity; it doesn't kill the app.
If you want to accomplish something similar to pressing the home button (so your app is hidden, but the activity is still alive), you can do something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
It's a little different than pressing the home key, because it will only take you to last app you were in when you launched this app. So it won't necessarily take you back to the launcher. But this behavior will be closer to what the user expects when pressing back.
You may also want to put this in the Manifest under your root Activity :
android:alwaysRetainTaskState="true"
This will prevent Android's default behavior of finishing all your Activities if they have been idle for about half an hour.
add this to your activity
public void onBackPressed() {
//finish(); this is what would normally happen
}
This way you have the full control on the back key. As for the home key, you better not do anything to it. otherwise it could be a big problem if something goes wrong with your app, and the user wont be able to get out of it. Also if you look at how android works, when a home key is pressed, the app isn't killed, just paused, unless the system decides there is not enough memory to keep your app, in which case it will shut down.
how to override backpress to not kill activity?
I believe that you have not understood certain things here. When you press your back button it doesn't mean that you are killing your app. It means that you are finishing your activity and which also means that your app is running in the background. If you want to kill your activity you have to do one of these,
by calling System.exit(0);
Or by
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
Or by manually getting into settings->manage applications->select your app->click on force stop
When you press your home key it means that your activity is paused or something like freezed and when you get to your app again, you can see your app resumed from where you left your app.