Android stop users from leaving fragment - java

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

Related

How to completely destroy an activity from code?

I am building an medication reminder app in android using java. User can set one or more reminders and according to those will get notified to take the medicine.
Problem is, whenever a notification is generated, and the user taps on it and the receiver activity opens up, the user is presented with two choices of either taking the medicine or skipping it. Now, I have made sure that in both the cases, the activity will finish and have called its onDestroy() method too. My objective is to prevent the activity from appearing in the recent app list, so that the user can not repeatedly take or skip medicines.
Here's the details(the links all point to screenshots of the app if it helps in any way):
The notification comes,User taps on the notification and the Reminder Receive activity opens up
User either takes or skips the medicine, and the activity finishes.
but the activity is still being shown on the recent app list, and if tapped on it, it opens up the reminder receive activity again, with the user able to perform the same action again
I want to prevent this particular behaviour from happening.
Here's the things I have tried(I have a fragment,running on top of the activity):
finishing the activity from fragment,then calling onDestroy() on it
ReminderRecieveActivity activity = (ReminderRecieveActivity) requireActivity();
activity.finish();
activity.onDestroy();
Modifying the onDestroy() method of the activity like so
#Override
protected void onDestroy() {
super.onDestroy();
int id= android.os.Process.myPid();
android.os.Process.killProcess(id);
System.exit(0);
}
But still the problem persists, Please help.
Why not create a separate Activity with android:noHistory="true" and also android:excludeFromRecents="true"? You don't have to kill the process to do that.

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();
}

App hangs on Camera.release()

I have an application that calls a custom activity within it for the purpose of recording audio and video. It uses the MediaRecorder and the Camera classes. My issue is that when the user hits the back button or cancel while the video is recording, the activity always hangs on Camera.release(), but if the stop button is pressed first, there's no problem. Here's my code:
Stop button code:
mRecorder.stop(); // Stop recording
mRecorder.reset(); // Reset recorder
camera.stopPreview();
onDestroy():
mRecorder.reset(); // Release media recorder
mRecorder.release();
if (camera != null) {
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
}
mRecorder = null;
camera = null;
I tried adding mRecorder.stop() to onDestroy() as well but that didn't solve it. I checked here but none of the answers worked. I'm really stumped with this one.
Code such as this should be put in onPause() and not onDestroy() to be sure it gets called when needed.
According to the docs
Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.
You aren't saving data here but the same principle applies. So stopping the recording in onPause() will guarantee the code runs when the user presses the Back button.
Overriding onBackPressed() would work too but that wouldn't account for if the Activity went into the background for other reasons.

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!!

Create new instance on launch

I'm fairly new to android, but I've noticed that pretty much every tutorial begins with this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
But I don't want the state to persist throughout relaunching the app. I want the user to start at the beginning if they relaunch the app. How can I achieve this?
In Android, state management is up to you during the Activity lifecycle. The onCreate, onPause, onDestroy and other lifecycle methods are all available to save and restore activity state and do other things. If you don't want your app to save any state, whether it be text boxes or animations or what not then don't capture it and restore it in these events.
Bundle savedInstanceState is only set by you when the activity is paused or stopped allowing you to store state and grab it in the onCreate or onResume methods, but that also happens on orientation change of the layout (the user tips from portrait to landscape) and then you probably do want to save state details in that Bundle.
And of course you could always reset any fields overriding onResume.
Also, the Activity Launch Modes might be worth looking at. A lot of times I will set my main activity to singleTop to have only one instance launched at any given time.
protected void onPause()
{
finish();
}
It will kill the activity as it pauses.and it will create a new instance each time.

Categories