Is there anyway to check if the application is minimized or if you have locked your device?
Because when I do minimize / lock my device the application is still runnig, this is mainly because I'd like to pause the music / sfx not to annoy people.. Like if someone is calling.
I am using Activity and SurfaceView with threads.
I have tried putting my pause method in the surfaceDestroyed / surfaceChanged but without success.
You should understand the activity lifecycle first
when the activity comes int the foreground it'll enter onPause() and it'll enter onResume if the user returns to the activity, an example to use onPause is like this
#Override
public void onPause() {
super.onPause(); // Always call the superclass method first
// DO YOUR STUFF HERE
}
}
for furthere reference about onPause you can see it here -> onPause Tutorial
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 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
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();
}
When the app is launched, Application onCreate is called. How to detect when the app is brought to front from running in background?
Look for onResume() method. Its is always called when your app comes foreground.
As per google docs:
The foreground lifetime of an activity happens between a call to
onResume() until a corresponding call to onPause(). During this time
the activity is in front of all other activities and interacting with
the user. An activity can frequently go between the resumed and paused
states -- for example when the device goes to sleep, when an activity
result is delivered, when a new intent is delivered -- so the code in
these methods should be fairly lightweight.
CODE SAMPLE:
#Override
public void onResume()
{
super.onResume();
Log.d("tag", "This screen is back");
}
You can override onResume().
#Override
public void onResume()
{
Log.d("tag", "This screen is back");
}
However, I would agree with the comment that you probably should look more into this to see how Android works.
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.