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.
Related
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
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
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!!
This question already has answers here:
How to detect "Recent Apps" system button clicks (Honeycomb+)
(8 answers)
Closed 9 years ago.
How can I detect when I press the return application button in android? it is on the right side of the home button on samsung s3. sorry I dont really know what is the name of the button.
Not the back button of the keyboard
It is called the Back Button. You can override its method to handle click events. Something like
#Override
public void onBackPressed()
{
// do stuff
super.onBackPressed();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.i("MainActivity", "Back button pressed, exiting..");
//Your code here..
}
return super.onKeyDown(keyCode, event);
}
Like this. i checked if it is back button. you can look up for the right button constant inside 'KeyEvent' class.
I guess you mean the button for opening the Recent Apps (the one on the right in the image):
There's no way to overriding it. The only method you can intercept is the Activity's onWindowFocusChanged, which is called once the recent apps is displayed, but also is been called on a lot of other different situations.
Take a look here for more info:
How to detect "Recent Apps" system button clicks (Honeycomb+)
And here if you want to avoid opening the recent apps (but it seems it doesn't work on Jellybean and up):
https://stackoverflow.com/a/17095749/1991053
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 :)