Reputation: 301
I have a lot of variable like isOnPause
, isOnStop
, etc. When the Activity called onStop()
, I let isOnStop = true
, I want to know if there exists an official method like this.isOnPause(), this.isOnStop()
?
I have check the API reference of Activity, only see isDestroyed()
, but no isStoped(), isPaused()
.
protect final void onStop(){
isOnStop = true // I don't think this is good way to do this.
}
so, I want to know if there exists an official method like this.isOnPause(), this.isOnStop()
?
Upvotes: 0
Views: 1315
Reputation: 443
Based on your post it seems like you want some other component to execute some code based on the lifecycle state rather than placing that code directly in the Activity/Fragment lifecycle callbacks directly? So are you looking for a way to tell another component what the state is of a particular lifecycle owner so that another component can act on it?
If so then another eay to make another class or component aware of the lifecycle state of a lifecycle owner is to implement LifecycleObserver. Classes that implement the interface can then be registered as an observer of a lifecycle owner
public class ConnectionListener implements LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void connectListener() {
...
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void disconnectListener() {
...
}
}
Then you can register that class as an observer of a lifecycle owner, such as an Activity or a Fragment
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//..
getLifecycle().addObserver(new ConnectionListener());
}
Here's the official docs for it: https://developer.android.com/topic/libraries/architecture/lifecycle#java
Upvotes: 2
Reputation: 14860
No, I don't think the Android platform exposes any API to explicitly know when an Activity is "stopped" or "paused". You need to keep track of it if you really really need it.
I have never found the need to keep track of an activity's (or fragment's lifecycle), at least not in this manner. I simply respond accordingly to those events. The approaches you can take vary based on your circumstances.
When an object referenced by an activity is only useful when the activity is in the foreground and visible to the user then use onResume
and onPause
When you need that object even if the activity is in the background, you "could" dispose the object in onDestroy
assuming this object can be garbage-collected, because even if onDestroy
never gets called the system will claim the memory used by this object. Again, assuming it can be garbage-collected
If the object can't be garbage-collected. Then you can either make it lifecycle-aware by implementing the LifecycleObserver
interface. More info in Android Developer Guides
You can also pass the activity as WeakReference
to an object that needs to interact with the activity and ALWAYS check for isEnqueued
and make sure that get
doesn't return a null value.
See below...
if(activityReference?.get() == null || activityReference?.isEnqueued == true){
//The activity has either been garbage-collected or is enqueued for garbage collection
}
EDIT: as per ianhanniballake's answer you could use getLifecycle().getCurrentState()
to determine the current state of the activity
Upvotes: 1
Reputation: 200130
When extending FragmentActivity
or AppCompatActivity
, you can access the current lifecycle state by using getLifecycle().getCurrentState()
. This returns one of the Lifecycle.State enum values, which have an isAtLeast() method, allowing you to write code like
Lifecycle.State state = getLifecycle().getCurrentState();
// The state could be either STARTED or RESUMED
boolean isStarted = state.isAtLeast(Lifecycle.State.STARTED)
// Paused means we aren't resumed
boolean isPaused = !state.isAtLeast(Lifecycle.State.RESUMED)
Upvotes: 5
Reputation: 456
You can see onPause() and onStop() Override methods in android Activity Lifecycle.
Upvotes: -2