There's a situation I have that's related to the Activity stack but I'm new enough to Android dev to not now how to respond to it.
I have an application that works like a stopwatch. When you start it, an ongoing notification goes in the notification tray and remains ongoing until you stop the stopwatch. I've noticed that if my stopwatch is running in the foreground, and I touch the notification, it creates a new instance of my stopwatch's activity and slides the old one off screen. This creates problems with the back button (you press back and the new instance goes away to reveal the old instance). It makes sense why this happens, but I don't want it to happen like this. What can I do to prevent multiple instances of my application from running?
What can I do to prevent multiple instances of my application from running?
In your Intent you use with the PendingIntent for the Notification, add setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); or setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);, whichever behavior fits your needs better.
You could also disable the back button. Not as elegant as CommonsWare solution but nothing wrong with some options!
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
return true;
}
return super.onKeyDown(keyCode, event);
}
Related
I am trying to make a simple project that could move the application to background by using
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
moveTaskToBack(true);
}
return super.onKeyDown(keyCode, event);
}
and now I am trying to bring the application to the front, displaying on the screen automatically even though user didn't reopen the application
For example, I set the timeout as 30 seconds, then I close the application (which is moved to background actually), after 30 seconds, the application will automatically move to front and shows text "Time Out". If I am playing games during time out, the game will pause and display the Time Out page, I tried to search for the solutions but the result is quite disappointing. the below are the codes I using now to bring the page to front, but it is only display in the app, unless user reopen the app then onli they can see the Time Out page otherwise they will not know
Intent intent = new Intent("com.lolcash.lol.PopOut");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
is there any other way to do that?
Try to create a service, this will run in background. When you want to bring the activity to front you can start it from the Service.
Another solution would be to display a notification with a message, when the user will tap it you can take him to the app.
I would like to disable the side volume buttons so the only way to control the volume will be from a dedicated activity inside my android app.
I managed to disable it for all my activities by adding the following code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "onKeyDown = " + keyCode);
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
return true;
}
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
return true;
}
return super.onKeyDown(keyCode, event);
}
But I don't know how to disable it for the activities i start from my app (for example I start the gallery app)!
I know it is possible as 'Volume Locker' app doing similar stuff.
It is not possible within public APIs to suppress the key events outside of your own Activities, if there is an app that has managed to do it what they are doing would be considered malicious by the platform designers and will get fixed at some point.
Based on the description given for that app (note: I've never used it personally)
Prevent accidental changes to your volume settings, install Volume Locker today.
This app helps prevent against accidental volume changes by confirming the change you made, by either tray notification or a pop up. If you don't approve the change, the volume will be reset within a set amount of seconds... By setting the timeout to "instant", the locked volumes will revert instantly without prompting.
I suspect what that is actually doing is listening for the volume buttons using a similar technique to the one in this answer and just reverting whatever change was made instantly(ish). That would make it seem to the user like the key press did nothing but in reality what happened is the volume changed and then quickly changed back.
When I test my android application on my phone, the application don't want to quit and make a bug on my phone. My little app take 70MB on my phone and still alive all the day...
How can I close it?
Do I have to put a Listner for the button "return" or there is some methods made from the SDK???
Thanks !
EDIT:
My application still runing, even if I press "HOME" ... this is not normal, is it?
Application on mobile aren't meant to quit, because it's against the UX of mobile user.
Have a look at this discussion. Android: Is quitting an application frowned upon?
you can override onKeyDown function like this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
}
return super.onKeyDown(keyCode, event);
}
I've been having a problem with my app racking up different pages in the history. Basically there is only three layers to my app and just going in between the 1rst layer and the 2nd layer if you use the app for 5 mins will cause you to to press the back button 30 times to exit the app. I am currently using intents to change the class:
Intent filterIntent = new Intent(view.getContext(), NewLayer.class);
startActivityForResult(filterIntent, 0);
I basically just want the user to press the back button 3 or 4 times and be able to exit versus pressing back going back through their entire browsing history to exit. Sorry if this is confusing question but I don't know how to properly phrase the question or the technical aspects of the issue to better reference what I mean.
When defining your intent, you might consider adding a flag to exclude the activity from being in the history.
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
There are other flags you might wish to add, such as FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS. The page I linked includes several you may wish to learn about.
Please write the below code in NewLayer.java file.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
Intent i = new Intent();
setResult(CANCEL_CODE,i);
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
I have a service running (Socket), this is how i start the service.
Intent s = new Intent(this, Socket.class);
startService(s);
in every activity i check for the user to select the home button, as soon as the home button is clicked i need to destroy the socket, so i have the below code on every activity in my app:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_HOME)
{
Intent s = new Intent(this, Socket.class);
stopService(s);
}
return true;
}
but this doesn't seem to stop my service?
Am i missing something? I need to destroy my service as soon as the home button is clicked.
Instead of hooking into keypress events and such, perhaps working with the built-in events like onPause, onStart, onDestroy, etc is more suited to your needs?
Another question on StackOVF recently had a brilliant reply with a flowchart that can help you figure out where to start/stop any other stuff you're using:
http://developer.android.com/images/activity_lifecycle.png
creds to monoceres for posting that in this topic:
App crashes after receving phone call