How can I detect when a user exit my app? - java

I'm writing an android app and I want to remove the user from an array when he exit the app. I tried using the onStop() method, but it's not woking as I desire, because whenever an activity changes, the user is removed from the array.
I need a way to execute a method when the user has removed the app from "recent activities".
Any suggestions on the matter?

You can track the back key so when the user presses it to exit the app you do what you want.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
//YOUR CODE HERE
finish(); //<<<------MUST BE USED SO THAT THE BUTTON WILL ACTUALLY FINISH THE APP
}
return super.onKeyDown(keyCode, event);
}
Otherwise you can try the onPause() method instead of the onStop. Android doesnt recommend using the onStop to save data etc. I think though that the back key is best

I need a way to execute a method when the user has removed the app
from "recent activities"
When you swipe an app from the list, it actually just stops all the app's services (and not the app itself!). You may want to consider creating a service just for this purpose so that when the service is killed, you are notified via onTaskRemoved.
You can read some more here

Related

Android stop users from leaving fragment

Is there any way to stop users from leaving a fragment (clicking back button, navigating to other fragment from navigation, etc)?
The situation is that when the user clicks to backup files to USB device, backup process is started (Thread) and if he leave this fragment, some awkward things happen to the backup process and the app crashes.
I was thinking if I could use some of the lifecycle callbacks like:
#Override
public void onPause() {
super.onPause();
//Do something to stop user from leaving
}
or
Is there another way to handle this situation and provide the best possible UX (maybe some dialog fragments involvement)?
you can override the method on the back with help of a flag indicating if the backup process is done or not
override fun onBackPressed() {
if(backUpFinished)
super.onBackPressed()
}
but, your saving backup shouldn't be related to the fragment

Android - calling finish() on activity does not stop it from running

So I've looked up similar problems to this, and have followed the advice in those threads, but it seems to give me no change in behavior.
So I'm writing an app that essentially notifies the user when they're going too fast or too slow based on GPS onLocationChanged() calls. I overrided the onBackPressed() method to finish() and return but the activity continues to run in the background when I go back to the main activity.
To be clear, I DO want the activity to run when the app is minimized or screen is off. I only want it to stop when the user goes back to the menu (IE, hits the back button)
Here's the link to the class on pastebin:
http://pastebin.com/V7z5c3HH
Thanks for your help! =D
Unsubscribe your location listener in the onDestroy method.
However, what you need for your GPS processing is probably a Service, not an Activity.
You need to remove the updates for that listener.
#Override
protected void onDestroy() {
locationManager.removeUpdates(locationListener);
super.onDestroy();
}

Android screensaver in app if it's not used

I created an android app. The screen never turns off:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Now I want to show a picture if the screen is not pressed for 5 minutes or something else. The app should not be closed, when pressing on the image the app should be open.
How can I realize that?
I would discourage you from taking this approach. Users expect to have a consistent user experience between various apps on their devices, and likely have a preference to how their device sleeps, either by having specified a sleep timeout or displaying a daydream as introduced in Android 4.2.
If you'd like to provide users with the option to display a screensaver associated with your app, I suggest including a Daydream in your app and otherwise acknowledging the user's preferences.
That being said, if you cannot use Daydream, you could observe if the app is being used or not. Two things come to mind:
Have the root view of your activity intercept touch events to observe if any of its children have been touched.
Observe the activity's onPause() and onResume() to acknowledge that the activity is still being displayed.
You could then invoke a Runnable by posting it to a view using postDelayed(Runnable action, long delayMillis), being wary to remove it when the activity is paused or the timer should be reset using removeCallbacks(Runnable).
I solved the problem!!!
I used that event:
public boolean dispatchTouchEvent(MotionEvent ev)
{
super.dispatchTouchEvent(ev);
// cancel my Timer
return true;
}
Thanks!!

Android Back Button reacts too quickly

I'm developing an android application and when I press the back button from my device (normal press time for a person, 1 second or less), it skips from my activity, to the previous activity (menu) and then exits the application.
But if I tap the back button quickly, it reacts as expected, it goes to the menu.
I've tried to find a solution but no success.
I've always tried to override the back button default behavior but no success either.
Is there a way to set a reaction time for the back button to react?
Many thanks in advance!
P.S.- I have other activities that maintains the expected behavior in back button when pressed with a normal press time.
"Is there a way to set a reaction time for the back button to react?"
Yes, you can simply record the time when the button is pressed and react differently in onBackPressed by calculating the (currentTime-lastTimePressed)
To allow this to work with previous activities, you can ask activities to startActivityForResult, so that when you finish your activity you can pass on the time as well to let them know if they should exit as well.
I was developing an extra option for an application that already exists, and I found out that I should extend not from the Activity from Android but an already extended Activity called SEActivity. So in this extended version of Activity they override the method onKeyDown like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
else
return super.onKeyDown(keyCode, event);
}
By extending this SEActivity, the Back button works with the expected behavior.
Thanks anyway :)

Block the application in Android

For my application I have implemented a system that detects if the current version is an old version that requires an update. By the way the detection work as intended.
My problem is how to block the application use when that happens. By now I'm launching an activity for inform the user and grant a link to the Market. My problem is that if the user press the back button returns to the last Activity and I dont know how to change the
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
} else {
return super.onKeyDown(keyCode, event);
}
}
For exit the application when the user press backs and don't go back to the other activities.
Thanks!
You can call finish() on the other activities, but I'm not sure how you'd get a reference to them.
To my knowledge the Android system does not let the programmer end tasks (which contain multiple activities in their back stacks), which is what you're trying to do.
You could try using Fragments instead of Activities, which will let you communicate information and events between them.
Before laucnhing the new activity, make sure to call finish() on main activity (outdated version of main app) to remove from back stack.

Categories