在选项卡之间共享对象(不同的活动) [英] Sharing an object between Tabs (different activities)
问题描述
我正在尝试为问题提供最佳解决方案:我正在开发一个带有3个选项卡的应用程序.我从rest服务获取xml数据并将其解析为一个对象(只有一个请求).现在,这三个选项卡显示此数据的不同部分.我当时正在考虑将应用拆分为不同的活动,以使代码更具可读性.如何在两次活动之间共享日期?我知道这个问题被提出了一百万次,但我仍然无法提出解决方案.
I am trying to come up with the best solution for a problem: I am developping an app with 3 tabs. I get xml data from a rest service and parse it into an object (there is only one request). The 3 tabs now display different parts fo this data. I was thinking about splitting the app into different activities to make the code more readable. How do I share the date between the activities? I know this question was posed a million times but I still cannot come up with a solution.
-
应用程序对象需要从Application类派生,但是我的主要活动已经从TabActivity类派生.使用另一个主类,然后以意图开始我的选项卡类?
application object needs to derive from the Application class, but my main activity is already derived from the TabActivity class. use a different main class und then start my tab class with an intent?
对对象的弱引用的哈希映射.似乎浪费了记忆,但有可能.
A HashMap of WeakReferences to Objects. Seems a waste of memory but would be possible.
将所有代码放入一个活动中并完成它.
Put all the code into one activity and be done with it.
感谢您的帮助:)
推荐答案
数据:
import android.app.Application;
public class Data extends Application {
private int blupp = 0;
public void setBlupp(final int bla) {
blupp = bla;
}
public int getBlupp() {
return blupp;
}
}
通过一项活动的oncreate()方法设置数据:
Setting the data in the oncreate() method of one activity:
final Data myData = ((Data) getApplicationContext());
myData.setBlupp(12);
通过另一个的oncreate()方法获取它:
Getting it in the oncreate() method of another:
final Data myData = ((Data) getApplicationContext());
final int test = myData.getBlupp();
在android清单中:
In the android manifest:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name="Data">
班级数据必须放在此处.那很简单.这个论坛的格式有点混乱.我不太了解代码格式. :(感谢您的所有帮助.
The Class Data must be put there. That was rather simple. Formatting is a bit messed in this forum. I don't quite get it with the code format. :( Thanks for all the help.
这篇关于在选项卡之间共享对象(不同的活动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!