Android如何处理多个实例数据/身份和JNI [英] Android How to handle multiple instances data/identities and JNI

查看:115
本文介绍了Android如何处理多个实例数据/身份和JNI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个良好实践和智能解决方案的问题,我需要一个建议。

This is a question of good practice and a smart solution, I need an advice.

我有一个应用程序(据我在Stackoverflow和谷歌搜索中可以看到):

I have an app that (and as far as I can read here in Stackoverflow and by Google search):


  • 该应用程序处理文档类型,我希望
    可以同时处理多个文档
    。 (我习惯于Win32,其中有一个程序段和每个实例的一段数据,但在Android / Java中显然不是这样。)

  • 我看到一个实例启动了应用程序存储(平板电脑)中的应用程序打开带有附加文档文件的Gmail或电子邮件,第三个实例是通过从文件处理应用程序(如ES文件资源管理器)打开文件。我喜欢他们都可以在两者之间翻转。用户可能希望一次读取多个文档。 (如果我在Android / Java环境中使用错误的单词实例,请更正我)

  • 该应用程序内置于包含所有数据和逻辑以及Java Android的JNI部分用户界面。(JNI部分设​​计为独立于OS,用于不同操作系统中的实现,具有粘合c文件。)

  • Android部分每次重新显示屏幕翻转或实例翻转

  • 只有一个JNI实例,即使重新创建Android Java部分并且所有Java数据都被清除,它仍然保留,现在它显示了最后一个在所有情况下读取文件在推送正在运行的应用程序按钮之间翻转

  • 只要可以将它们绑定到每个实例,在JNI部分中创建不同的实例都没有问题。 Java实例,带有身份或我可以用作与JNI部分交换的参数,但是

  • 我无法在每个实例中保存FilePathName以识别Java部分中的实例,因为在重新创建Java部分时将擦除它。

  • The app handles kind of documents and I like it possible to handle more than one document at the same time. (I am used to Win32 where there is a program segment and one segment of data for each instance but that is obviously not the case in Android/Java.)
  • I see one instance starting the app from the app storage (the tablet) another opening a Gmail or email with an appended document file, a third instance by opening a file from a File handling app like ES file explorer. And I like them all be possible to be flipped in between. The user might like to read more than one document at a time. (correct me if I use the word instance wrong in the Android/Java environment)
  • The app is built in a JNI section that contains all the data and logics and a Java Android user interface. (The JNI section is designed to be OS independent for implementations in different OS, has a glue c-file.)
  • The Android section recreates every time the screen is flipped or instances are flipped between
  • There is only one JNI instance and that is kept even when the Android Java part is recreated and all Java data is wiped out, right now it shows the last read file in all cases flipping in-between pushing the running app button
  • There are no problems making different instances within the JNI section as long as it is possible to bind them to each Java instance, with an identity or something that I can use as a parameter in the interchange with the JNI section, but how?
  • I can't save for instance the FilePathName in each instance to identify the instance in the Java section because it will be wiped when the Java section is recreated.

第一个问题是我在阅读Stackoverflow和Googled文章的观察中是否正确?

First question is if I am right in my observations reading Stackoverflow and Googled articles?

第二个问题,解决问题的任何好建议?我需要建议


  • 有吗是否有可能在所有情况下都能识别实例?

  • 任何其他可能的路径,包括为每个实例分离数据或将JNI的实例标识为处理每个实例的数据?

推荐答案

Jan

我们在应用程序中遇到与JNI对象类似的问题。问题是JNI链接不能像普通的Java对象一样工作,必须明确地解决。与此同时,我们的活动可以随时被Android销毁。

We have similar problems with JNI objects in our application. The problem is that JNI link isn't work as ordinary Java object and has to be relesed explicitly. At the same time we have activity that can be destroyed at any moment by Android.

我们当前的解决方案是将JNI对象存储在应用程序级别,并具有管理引用和删除的可能性参考为零时的对象。如果活动将永远被破坏,也会破坏JNI参考。所以这就像你在上一篇文章中所做的那样。

Our current solution is to store JNI objects on Application level with posibility to manage refereces and drop objects as soon as reference is zero. And also destroyed JNI reference if activity is going to be destroyed forever. So this is similar like you did in previous post.

但是,如果您希望在一段时间后让您的应用程序可扩展,您可能会理解此解决方案并不理想。

However if you would like to have your application scalable after some time you might understand that this solution isn't ideal.

Android系统有时会临时破坏活动以节省内存。在您的情况下,所有带文档的JNI对象仍将消耗内存。因此,这里最好的解决方案是能够将JNI级别的文档保存为捆绑状态。如果您的文档可以由用户更改,这一点尤为重要。在这种情况下,通过在onSaveInstanceState中保存JNI对象的状态,您可以销毁JNI对象并在onCreate中重新创建。然而,在这里分析需要多少时间来销毁/创建保存到捆绑文档的JNI对象是非常重要的,因为我们必须在某些情况下(例如纵向/横向模式)快速支持活动重建,并使用一些有限的捆绑(不超过1Mb) )。如果进程很长,这个解决方案可能不太好。
另外,您希望有一个任务 - 一个文档系统。当您在一个任务中有多个活动时,您应该考虑这种情况。

The first thing that Android system sometimes temprorary destroys activity to save memory. In your case all JNI objects with documents will still consume memory. So the best solution here is to be able documents on JNI level saves its state to bundle. This is especually important if your documents can be changed by user. In that case by saving state of JNI object in onSaveInstanceState you can destroy your JNI object and recreate in onCreate. However here it is important to analize how much time is required to destroy/create JNI object with saved to bundle document as we have to support quickly activity recreation in some case (portrait/landscape mode for example) with some limited bundle (not more 1Mb). In case of long process this solution might be not good. Also as you would like to have one task - one document system. You should consider case when you have several activities in one task.

Android不会始终调用onDestroy()的第二项。如果你做了一些保存操作,这里的数据有时会丢失。

The second item that Android isn't call onDestroy() always. And if you do some save operation here data might be lost sometimes.

希望这些信息可以帮到你。

Hope this information helps you.

这篇关于Android如何处理多个实例数据/身份和JNI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆