分享活动的对象 [英] Sharing an object between activities

查看:137
本文介绍了分享活动的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四项活动一个天气应用程序。主/发射活动是隐形使用...

I have a Weather app with four Activities. The main/launcher activity is 'invisible' using...

安卓主题=@安卓风格/ Theme.Translucent.NoTitleBar`

android:theme="@android:style/Theme.Translucent.NoTitleBar"`

...而只是用来做一些检查(这是否是一个新的安装,网络连接是否可用等)发射过的其他活动的人之前。其他的活动是面向UI - 两个简单地显示从网站和第三方提供的位置选择器,以便用户可以选择哪个区域,显示天气拉升气象数据

...and is simply used to do a few checks (whether this is a new install, whether a network connection is available etc) before firing off one of the other Activities. The other Activities are UI-oriented - two simply display weather data pulled from a website and the third to provide a location 'picker' so the user can choose which area to show the weather for.

但是,所有四个活动,利用一个WeatherHelper对象基本上做的一切,从检查的SD卡存储,以维持preferences拉/格式的网页的。

However, all four activities make use of a WeatherHelper object which basically does everything from checking for available SD card storage to maintaining preferences and pulling/formatting website pages.

所以,我的问题(S)...什么是有WeatherHelper的一个实例可以使用多个活动和在哪里/如何最好地在我的情况下创造的呢?最好的办法

So, my question(s)...what is the best way to have one instance of WeatherHelper which can be used by multiple activities and where/how are best to create it in my case?

我是一个面向对象的编程了很多年,但我是很新的Andr​​oid和设计理念 - 我读了很多关于Android开发者的网站在过去的几个星期,但我已经停滞努力决定这一点。

I've been an OO programmer for a lot of years but I'm very new to Android and the design concepts - I've read a lot on the Android Developers site over the past weeks but I've stalled trying to decide on this.

任何想法感激地接受。

推荐答案

我会保存在您共享的信息应用对象。继承这一点,并添加任何额外的初始化和数据存在。你可以让你的应用程序中使用<一个href="http://developer.android.com/reference/android/app/Activity.html#getApplication%28%29">getApplication()从你的行为,你可以投你的专业版和访问共享数据。

I would store shared information in you Application object. Subclass this and add any extra initialization and data there. You can get your application using getApplication() from your activity, which you can cast to your specialized version and access the shared data.

我也将避免发动特殊的启动活动如果可能的话,做的工作在你的应用程序的onCreate()重写。

I would also avoid launching the special startup activity if possible and do the work in your Application's onCreate() override.

这篇关于分享活动的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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