简体   繁体   English

有什么方法可以调用清理函数,该函数还清理本机代码,同时在Android中保留任何活动?

[英]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. 转到其他活动时调用了onDestroyonStop

Maybe some of these methods will be useful for you. 也许其中一些方法对您有用。

  1. For the transition from tempActivity to mainActivity , put a call to the clean up function in tempActivity 's onDestroy method. 为了从tempActivity过渡到mainActivity ,请在tempActivityonDestroy方法中调用清除函数。
  2. For the case where the entire application is exited: that is much more messy. 对于整个应用程序都退出的情况:这更加混乱。 There is no global function that tells when an app exits, so you'll need to use some other means of tracking that the application is exiting. 没有全局函数可以告知应用程序何时退出,因此您需要使用其他一些方式来跟踪应用程序正在退出。

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. 我建议使用ServiceonStartCommand设置为使用功能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.

相关问题 Android NDK:重启活动后如何清理本机代码? - Android NDK: How to clean up native code after restarting activity? 有什么方法可以在 React 本机 android 中检测最近的电话通话时间 - Is there any way to detect recent phone call duration in React native android Android中的OnClick函数无法启动任何活动 - OnClick function in Android not starting any activity 有没有可能有原生的Android代码? - Is there any possibilities to have native android code? 如何在Android Studio中将选中的“复选框”的结果显示为一个活动而又不留空白? - How to display the result of checked “check boxes” into one activity without leaving any blank spaces in Android Studio? 有没有办法在没有安装sl4a的情况下在android开发中调用python代码? - Is there any way to call python code in android develop without installing sl4a? 重构不会更改任何源代码ANDROID ACTIVITY ERROR - The refactoring does not change any source code ANDROID ACTIVITY ERROR Android JNI本机C函数调用杀死了活动 - Android JNI native C function call kills activity 有什么办法可以在tomcat中仅部署一个项目,而将其他项目保留 - Is there any way to deploy only one project in tomcat leaving the other projects 有什么办法可以让程序在同一行上重复自己,同时也删除它之前写的内容? - Is there any way to make a program that repeats itself on the same row, while also erasing what it wrote before?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM