简体   繁体   English

在Android中重新打开应用程序后如何使用缓存的数组?

[英]How to use cached array after reopening app in Android?

It's my first Java coding experience, so I had to search a lot of information, however I still have a problem. 这是我的第一次Java编码经验,因此我必须搜索很多信息,但是仍然有问题。

In my app I must read a 4.5 Mb array to memory. 在我的应用中,我必须读取一个4.5 Mb的阵列到内存。 As I see from Memory Monitor in Android studio, it is not deleted after pressing back and exiting to the desktop. 从Android Studio的Memory Monitor中可以看到,按下并退出桌面后它不会被删除。 It is not deleted by GC. GC不会删除它。 Can I use the same array for the 2nd launch? 我可以在第二次发射中使用相同的阵列吗? The pointer to it (it's a global in MainActivity) is null every time, so I have to get a new array. 指向它的指针(在MainActivity中是全局的)每次都为空,因此我必须获得一个新数组。 But the previous isn't deleted. 但是前一个不会被删除。 If I don't set the pointer to it to null and not call System.gc() in onPause(), the app crashes with OOM. 如果我没有将指向它的指针设置为null并且没有在onPause()中调用System.gc(),则该应用程序将因OOM而崩溃。 I've tried putCharArray and getCharArray, still no use. 我已经试过putCharArray和getCharArray,仍然没有用。 Is there any way to use the same array? 有没有办法使用相同的数组? Or do I have to read it every time when I open the app? 还是每次打开应用程序时都必须阅读它? It happens even after just going to Settings and back from notification bar or turning on/off physical keyboard. 即使在进入“设置”并从通知栏返回或打开/关闭物理键盘之后,也会发生这种情况。 Android 4.0.3 AVD. Android 4.0.3 AVD。

Update Shame on me, I even haven't thought about indexing. 更新我的耻辱,我什至没有考虑过索引。 Anyway As I know, Android has an app in cache after closing it with Back button. 无论如何,据我所知,在使用“后退”按钮将其关闭后,Android在缓存中有一个应用程序。 If I just create an array, but don't make it null in onPause, the memory isn't freed by GC (as I can see in Memory Monitor). 如果我只是创建一个数组,但是在onPause中不将其设置为null,则GC不会释放内存(正如我在“内存监视器”中看到的)。 But after opening the app again the pointer is null, even if I didn't null it. 但是,再次打开应用程序后,指针为空,即使我没有将其为空。 Is there any way to save the pointer and this structred not in file and check if it's still in cache after opening the app again in order not to reload the file? 有什么方法可以保存指针,并且该指针不在文件中,并在再次打开应用程序后检查它是否仍在缓存中,以便不重新加载文件? Not after only killing the process, but even for just moving it from screen when, for instance, Settings are opened. 不仅是杀死进程,而且甚至是例如在打开“设置”时仅从屏幕上移走进程。

you either create a database and insert the data into it so it can be loaded on start or save it in your locally on the sd storage in a file, for an array i suggest this way and write the array into an json file: 您可以创建一个数据库并将数据插入其中,以便可以在启动时加载它,也可以将其保存在文件中sd存储中的本地文件中,对于阵列,我建议采用这种方式,然后将该阵列写入json文件:
https://stackoverflow.com/a/14706227/1173391 https://stackoverflow.com/a/14706227/1173391

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM