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);
}
Related
I have one problem in notification click event.
for example there is a four activity in application A,B,C,D.
Currently Activity B is open and i get notification ,when i click on notification i want to open activity D. and its its working fine but problem is when i click back button from actvity D, its open activity B.
I tried Intent.FLAG_ACTIVITY_CLEAR_TOP , Intent.FLAG_ACTIVITY_NO_HISTORY ,Intent.FLAG_ACTIVITY_SINGLE_TOP etc but still unsuccessful finish all intent.
please help me ,Thank you in advance.....
i found the solution, add flag in new Intent Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
If I understood your problem correctly, I think you have to override the back button, in order to give it a custom behaviour. Something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Go to previous activity, or some other place
}
return super.onKeyDown(keyCode, event);
}
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 know when the user pressed the home button while he was running my app.
BUT:
The problem is that I don't want to edite the existing code.
meaning I don't want to add logic to the existing activities onPause() method.
The only solution I found was to add a service to the application which listens to the Log detecting if there was an intent to run the
HOME: Starting: Intent { act=android.intent.action.MAIN
cat=[android.intent.category.HOME]
Is there any other way to do it or is it really impossible?
The optimal solution would have been to catch an intent in the Manifest.xml, like:
action android:name="android.intent.category.HOME"
and implement a new class to catch it. (But it doesn't seem to catch it).
I would like to know when the user pressed the home button while he was running my app.
What specifically are you trying to achieve?
BUT: The problem is that I don't want to edite the existing code.
By definition, that is impossible.
The only solution I found was to add a service to the application which listens to the Log detecting if there was an intent to run the
That is modifying the code, violating your own requirement. Moreover, it requires a permission that really you should not be asking for.
Is there any other way to do it or is it really impossible?
That depends on what specifically you are trying to achieve.
The optimal solution would have been to catch an intent in the Manifest.xml...and implement a new class to catch it. (But it doesn't seem to catch it).
HOME is a category. It is not an action. Home screens are activities that respond to the MAIN action in the HOME category. However, it is modifying the code, violating your own requirement.
You can use key press event handler
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
// Home key pressed
} else {
return super.onKeyDown(keyCode, event);
}
return false;
}
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);
}
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