I'm new to android development and I get how the android has an activity life.
If I have an app and I press a button to use the phone's camera funcationality like so...
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_VIDEO_REQUEST);
}
How does onPause() or onDestroy() and the other stuff work?
I have this outside the onCreate()
protected void onPause(){
super.onPause();
}
If I want to press the back button or press the home button, do I have to destroy or pause the camera function? If so, I'm still trying to figure out how to do so?
Thanks!
When you start new activity from your current activity then there are two possibility of your current activity
Pause
Stop
Paused:
Another activity is in the foreground and has focus, but this one is still visible. That is, another activity is visible on top of this one and that activity is partially transparent or doesn't cover the entire screen. A paused activity is completely alive (the Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager), but can be killed by the system in extremely low memory situations.
Stopped:
The activity is completely obscured by another activity (the activity is now in the "background"). A stopped activity is also still alive (the Activity object is retained in memory, it maintains all state and member information, but is not attached to the window manager). However, it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere.
For example you are starting the Camera activity from your activity then your current activity will be Stop because the Camera activity will covers all your screen and your activity is not visible to camera activity.
Here is the complete description.
You are starting the Camera activity using the Intent so you do not have to handle the call back methods of the camera activity. System will manage the call back method you do not have to manage it.You just have to manage the Activity result which you will get in your activity from the Camera activity.
EDIT
And also ob course you never have to directly call any life cycle methods of the Activity.System automatically calls this method according to activity state.You just have to write your implementation in this methods to do your work.
Related
What is the reason to overide onResume? Can we avoid it?
#Override
protected void onResume() {
super.onResume();
When is mandatory to override that method in MainActiivity?
Because sometimes I see it is used sometimes not.
first of all you must know about android lifecycle link https://abhiandroid.com/programming/activity-life-cycle
onResume();
can be called in two different places after onStart(); when the user start interacting with your activity and data in the activity is visible to him
or after onPause();
when your activity goes to the background then you can implement it here to add some logic which is needed for your application .
Please reference this link:
https://developer.android.com/guide/components/activities/activity-lifecycle#onresume
When the activity enters the Resumed state, it comes to the foreground, and then the system invokes the onResume() callback. This is the state in which the app interacts with the user. The app stays in this state until something happens to take focus away from the app. Such an event might be, for instance, receiving a phone call, the user’s navigating to another activity, or the device screen’s turning off.
When the activity moves to the resumed state, any lifecycle-aware component tied to the activity's lifecycle will receive the ON_RESUME event. This is where the lifecycle components can enable any functionality that needs to run while the component is visible and in the foreground, such as starting a camera preview.
When an interruptive event occurs, the activity enters the Paused state, and the system invokes the onPause() callback.
If the activity returns to the Resumed state from the Paused state, the system once again calls onResume() method. For this reason, you should implement onResume() to initialize components that you release during onPause(), and perform any other initializations that must occur each time the activity enters the Resumed state.
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.
I have an android activity. If the user switches off the monitor of the android device for some time and then switches back on again, It seems like that the current activity is restarted. I found an activity lifecycle picture on the Internet but it does not tell me what method is called when the user switches off the monitor. It only tells me that when the activity is running, and "another activity comes in front of the activity", the onPause() method is called. But a "black screen" is not "another activity", right?
So what is called when the device's monitor is switched off?
onPause will be called first, then onStop will be called
Activity lifecycle methods and state
onResume() called- Visible
onPause() called - Partially Visible
onStop() called - Invisible
If app is in memory, then onRestart call when app starts again, otherwise onCreate() called
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.
We know that the android activity cycle has multiple phases.
between onStart() and onStop() is called the visible lifetime
between onResume() and onPause() is called the foreground lifetime
What are the the key difference between them? Please give examples if possible.
If I display an Activity on the screen and the user is interacting with it, it is both in the foreground and visible.
If I start another Activity, which is transparent and shows a dialog box over the previous Activity, then the new Activity (the dialog box) is in the foreground and the old Activity is not in the foreground but still visible.
between onStart() and onStop() called visible lifetime that mean that the activity is visible either entire activity or partially visible and the user can see it on the screen and interacte with
between onResume() and onPause() called foreground lifetime that your activity is full visible and running and have full focus .
UPDATE
partially visible for example if another activity come in front of the current one and it only display a dialog and a transparent background . the user can see that activity but cant interact with it
Foreground Activity: You’d think what the user is currently interacting with would be the most important thing to keep alive.
Visible Activity: You’ll find that there are situations where your activity can be visible but not in the foreground. A simple example is when the foreground activity starts a new activity with Dialog theme or a translucent activity. Another example might when you invoke the runtime permission dialog.
Please find below link for better understanding
https://medium.com/androiddevelopers/who-lives-and-who-dies-process-priorities-on-android-cb151f39044f
The Visible Lifetime: Although this is termed the "visible lifetime", the app may not be directly visible and interacting with the user at any one time if it is not in the foreground. The feature that distinguishes this lifetime is that, even if not in the foreground, the app maintains resources such that it can instantaneously return to the foreground.
The Foreground Lifetime: During foreground lifetime the activity is in front of all other activities and interacting with the user.