简体   繁体   English

活动之间的全局对象

[英]Global Objects between Activities

at the moment i'm working on an android app, but i've encountered a problem which i cannot solve. 目前,我正在使用android应用程序,但遇到了无法解决的问题。 I have 2 activities. 我有2个活动。 In one of them i define an ArrayList as public static. 在其中之一中,我将ArrayList定义为公共静态对象。 This list is used as content for a listview. 该列表用作列表视图的内容。 in that other activity i want to edit items of that specific list or rather delete them. 在其他活动中,我想编辑该特定列表的项目,或者删除它们。

Problem: on my samsung galaxy S II it works fine (Android version 4.0.4), but on other devices, like SGSIII (4.1 i think) there is a problem with synchronizing that list. 问题:在我的三星银河S II上,它可以正常工作(Android 4.0.4版),但是在其他设备上,例如SGSIII(我认为是4.1),同步该列表存在问题。 so basically if that second activity finishes and returns to the first, all changes seem to be undone... 因此,基本上,如果第二项活动结束并返回到第一项活动,所有更改似乎都将撤消...

here is the code of the update function of the listview in the first activity; 这是第一个活动中listview的update函数的代码; the changes are not recognized in this method: 此方法无法识别更改:

private void updateList() { 

    String[] values = new String[hatCommandList.size()];

    for(int i = 0; i < values.length;i++) {             

        values[i] = ((Command)hatCommandList.get(i)).getRealCommand();

    }           
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);     
    ListViewSentCommands.setAdapter(adapter);       
}

as you can see, nothing special. 如您所见,没什么特别的。 and here the code of the 2nd activity in which i edit the "hatCommandList": 这里是我编辑“ hatCommandList”的第二个活动的代码:

public void onClick(View v) {               
    SettingsActivity.hatCommandList.remove(position);                       
    finish();

}

"position", which is used to point at a position in the list obviously, contains the right value, i checked it already. “位置”,显然是用来指向列表中的位置,包含正确的值,我已经检查过了。

Anyway, i hope you can tell me why it works on some device and why it doesnt on some... 无论如何,我希望您能告诉我为什么它可以在某些设备上运行,为什么它不能在某些设备上运行...

Greetings and thanks in advance, Timo 提前致以问候和感谢,蒂莫

Do not do this... 不要这样做...

SettingsActivity.hatCommandList.remove(position);

Most importantly DO NOT create public objects or methods in an Activity and attempt to use them from any other Android application component - an Activity is a special case class which has a life-cycle and should not be treated like a 'normal' Java class. 最重要的是,请勿在Activity创建public对象或方法,并尝试从其他任何Android应用程序组件中使用它们Activity是具有生命周期的特例类,不应将其视为“常规” Java类。

If you want to share data in this way then I would recommend you create a POJO helper class and hold the ArrayList there. 如果您想以这种方式共享数据,那么我建议您创建一个POJO帮助器类并在其中保存ArrayList The second Activity can then make changes and when the first Activity resumes, have it check for data changes and refresh the adapter. 然后,第二个Activity可以进行更改,当第一个Activity恢复时,让它检查数据更改并刷新适配器。

Synchronize your list: how to create Synchronized arraylist 同步列表: 如何创建同步数组列表

Probably your running into a race condition. 可能是您遇到了比赛状况。

Ok, i found the problem: 好的,我发现了问题:

when the first activity is started, it reads data from a file into that list. 当第一个活动开始时,它将从文件中读取数据到该列表中。 that happens in the onCreate function. 发生在onCreate函数中。

The problem is now, that on my SGSII device with android 4.0.4 the focus resumes to the first activity when i finish the second without problems. 现在的问题是,在我使用Android 4.0.4的SGSII设备上,当我顺利完成第二个任务时,焦点将恢复为第一个活动。

BUT on the SGSIII with 4.1 the onCreate method is used once again, even though it actually only resumes. 但是,在SGSIII上使用4.1的onCreate方法将再次使用,即使它实际上仅在恢复。 At the moment i have no idea why, but that's a problem which can be solved i guess. 目前我不知道为什么,但是我想这是一个可以解决的问题。

maybe you can give me some tips why the developer of android sdk did change and how i can stop that. 也许您可以给我一些提示,说明android sdk的开发人员确实进行了更改,以及如何停止该操作。

PS: i'm calling the startActivityForResult method to start the 2. activity and also the onActivityResult function. PS:我正在调用startActivityForResult方法来启动2.活动以及onActivityResult函数。

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

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