gpt4 book ai didi

java - 使用静态变量在包内的 Activity 之间共享数据

转载 作者:行者123 更新时间:2023-12-01 12:37:18 25 4
gpt4 key购买 nike

所以基本上,我无法摆脱这样一个事实:这似乎是一个坏主意,但我无法真正确定原因。

我有Activity AActivity BPackage 1Activity A进行 Volley 调用并创建模型列表 Model X包含一些简单的数据(字符串和基元)。举一个非常简单的例子,让 Model X表示一篇新闻文章(因此模型列表是新闻文章列表),其中包含新闻文章的标题和正文。 Activity A包含一个显示文章标题的列表。当您单击列表中的某个项目时,Activity B被调用,文章标题显示在顶部,文章正文显示在下面。 Activity B无法从除 Activity A 之外的任何地方调用并且仅在 A 检索到 Model X 后现在假设我们有大量这些模型,它们都包含大量简单类型,并且您不确定模型 Activity B 中有哪些数据。需要,你只要知道它需要 Model X检索于 Activity A .

“正确”的步骤似乎是让模型实现 Parcelable 并将模型以 bundle 形式传递给 Activity 。问题是我有很多模型和大量数据,如果我能节省时间那就太好了。我试图实现某种通用的包裹器,但这变得一团糟。所以这就是我的想法:

Could Activity A not save the model selected in onItemClick() as a protected static variable that Activity B can use without having to pass it in a parceled bundle since, once the model is made, it will be the only instance of the model and the model will not be altered in any way so the state of the model can't be disrupted by the static call?

类似这样的事情

Activity A

public class A extends Activity implements OnItemClickListener{
private List<X> mModels;
protected static X mModel;
private SimpleListAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// code

if(mModels == null)
requestModels();
else
buildInitialList();
}

@Override
public void onItemClick(AdapterView<?> parent, View child, int position, long id){
mModel = mAdapter.getItem(position); // <-- returns Model X in list of models
startActivity(new Intent(this, B.class));
}

/*
Some irrelevant code, including requestModels() network call (I have a
NetworkManager class that does that, but again it isn't important how it
does that, just important how I get my data) as well as building my list.
*/

// Listener attached to Volley call for response
private Listener<ArrayList<Model>> getVolleyListener(){
return new Listener<ArrayList<Model>>(){

@Override
public void onResponse(ArrayList<Model> models) {
if(models != null){
mModels = models;
buildInitialList();
}
else
closeOnEmptyList();
}

};
}
}

Activity B

public class B extends Activity{
private X mModel;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//code

this.mModel = A.mModel;
doSomethingWithModel();
}

private void doSomethingWithModel(){
String title = mModel.title;
String body = mModel.body;
// do something
}
}

The Model class X will never be used outside of Activity A and Activity B, and every Activity that needs to use a Model of the same type is placed in the same Package 。我有许多类可以做类似的事情(新闻文章、热门问题和答案、法律详细信息,大部分只是其他通用字符串数据)。我可以把所有不是问题的东西都打包起来,我只是似乎无法指出它出了什么问题,即使它看起来有缺陷。没有数据需要任何安全性,也没有模型大到足以导致静态堆内存出现问题。

编辑

我的案例并不完全独特,但有些不常见。我同意在应用程序重新启动时必须重新拉取我的数据。这意味着(据我所知)if I store the Model statically of the protected type in Activity A, there is no way that the Model could be null in Activity B因为静态变量仍然保留在应用程序中,从仍然暂停的Activity A开始或者应用程序已重新启动,用户将必须执行 Activity A重建我们的截击Model这样Activity B也可以使用它。 (这里需要逻辑检查可能有错误)

我确实意识到 volley 有一个缓存,但我相当确定它需要正确的缓存 header (给出类似 304 的响应代码),但我没有得到。对此的任何意见也将受到赞赏。再说一遍,我意识到使用静态并不完全理想,但我真的很想弄清楚为什么在这种情况下不应该使用它们,或者这是否实际上是它们的用途之一。

最佳答案

这是一个很好的博客,解释了详细信息,但如果您确实想在静态变量中存储一些数据,那么最好的选择是在应用程序类中使用该变量,但即使如此也存在陷阱。例如,您的 Activity 可能会暂停,然后使用应用程序的新实例恢复,并且您的数据将会消失(因此在某个对象上调用方法可能会给您带来 NPE。我认为使用可解析的方法更安全,或将其保存到一个 Activity 中的共享偏好并在下一个 Activity 中加载。

这是我提到的博客,它可能解释了如果您继续将数据存储在静态变量中以在 Activity 之间传递,则可能会出现问题的原因。

http://www.developerphil.com/dont-store-data-in-the-application-object/

关于java - 使用静态变量在包内的 Activity 之间共享数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25474821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com