Reputation: 21
So, on Android,
How can I listen to/subscribe to an Activity onDestroy()
method? I have a shared object/class/service that can be accessed and created through multiple activities but it needs to know when the caller activity is destroyed.
I can't modify the caller activities code, but I have a reference to the particular activity that is calling my class.
So someActivity -> Creates my object
Object needs to know when that activity closes.
Upvotes: 2
Views: 1111
Reputation: 742
I assume you are trying to do something in the object you created using your activity, but somehow, if your activity is closed. Then your object will crash if it try to access getActivity().
Assume your activity class name someActivity and your object is a fragment. Fragment provide you to use getActivity. If the object you created not a fragment, then you probably has to find a way access it.
someActivity activity = (someActivity) getActivity();
if (activity != null && !activity.isFinishing()) {
//do your action here to prevent the app crashes.
}
Upvotes: 2