[英]Is there any way to call clean up function that also cleans the native code while leaving any activity in Android?
see 看到
i have mainActivity
which calls tempActivity
我有
mainActivity
,它调用tempActivity
Now inside tempActivity i call some native method and malloc some data .. so now when user 现在在tempActivity内部,我调用一些本机方法并分配一些数据..所以现在当用户
1> come back from tempActivity to mainActivity or 1>从tempActivity返回到mainActivity或
2> exit from whole application 2>从整个应用程序退出
i need to call one clean up function which free all data malloc in native method so how can i implement such things? 我需要调用一个清理函数来释放本机方法中的所有数据malloc,所以我该如何实现这些东西?
Does android framework has any method to handle such clean up function? android框架是否有任何方法可以处理此类清理功能?
Ok, i think the question is about how to free the memory that is being allocated using malloc()
inside native code. 好的,我认为问题在于如何释放在本机代码中使用
malloc()
分配的malloc()
。
I had the same requirement in one of my Projects. 在我的一个项目中,我有相同的要求。 I did it in the following way.
我以以下方式做到了。
Inside onStop()
or onDestroy()
, I did a native call, which I have used to free()
the allocated memory. 在
onStop()
或onDestroy()
,我进行了本机调用,该调用已用于free()
分配的内存。 This I think is the best and simplest way to do that. 我认为这是最好,最简单的方法。 I am not sure if android is providing some API,s for this.
我不确定android是否为此提供了一些API。 It would be informatory to know if something like this exists.
知道是否存在类似信息将很有帮助。 Thanks.
谢谢。
See the diagram taken from here . 请参阅此处的图表。
Basically the Activity
class is defining several methods that can be of use to you: 基本上,
Activity
类定义了几种可能对您有用的方法:
onPause
which is called every time your activity is paused: that is your screen locks, you go to other activity onPause
:这是屏幕锁定,您转到其他活动 onDestroy
and onStop
called when you go to other activity. onDestroy
和onStop
。 Maybe some of these methods will be useful for you. 也许其中一些方法对您有用。
tempActivity
to mainActivity
, put a call to the clean up function in tempActivity
's onDestroy
method. tempActivity
过渡到mainActivity
,请在tempActivity
的onDestroy
方法中调用清除函数。 I'd recommend using a Service
with the onStartCommand
function set to use START_NOT_STICKY
and then in the onDestroy
method of the Service
call the clean up function. 我建议使用
Service
与onStartCommand
设置为使用功能START_NOT_STICKY
然后在onDestroy
的方法Service
调用清理功能。 If you do this and you have all Activity
s use the service then you can do it all in there. 如果执行此操作,并且所有
Activity
使用该服务,则可以在其中完成所有操作。
The recommended technique is to allocate whatever you need in onResume
and clean up in onPause
. 推荐的技术是在
onResume
分配所需的内容,并在onPause
清理。 This way you allocate when your activity is about to be shown and clean up when your activity has become partially or fully obscured by some other activity. 这样,您可以在活动将要显示时进行分配,并在活动被某些其他活动部分或完全遮盖时进行清理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.