java整个工程共享变量,关于java:如何轻松地与其他活动共享变量?

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:

How do I pass data between activities in Android?

我正在制作一个纸牌游戏,我有一个用于丢弃卡片的活动和一个用于显示分数的活动。 问题是我想将一些对象(玩家和经销商的手)传递给另一个活动,这样我就可以将得分中的imageViews设置为玩家手中的牌。 我怎样才能做到这一点? 我不关心安全性或任何我想要最简单的方法。

最简单的方法是使用捆绑概念

你如何使用bundle发送不是字符串或其他东西的对象?

不,因为他们只教你如何做基本的事情,如字符串......

在意图中使用捆绑包不是关于安全性,而是因为Android人员这样简单明了。在我看来,使用bundle和Intent来传递更大的对象并不是一个好主意。它实现起来太复杂了,让你把对象缩小到原语(当使用parcelable时),并在内存的另一边复制(你拿一个对象,在intent中设置所有内容然后重新创建它另一方制作了一个新的副本)对于内存占用较大的对象而言并不好。

我会建议:

要么使用单身商店

使用应用程序类(也像单例一样)

我经常使用一个单例,里面有一个hashMap,其中一个整数键由我(来自原子整数)和一个放在地图内的对象生成。您只需将意图内的ID作为额外内容发送,并通过获取意图中的密钥并访问您的单例来检索和删除对象(从该地图)并在新的活动/服务中使用它,从另一方面检索它。

以下是这样的示例:

(注意:这是我的lib中的一部分用于休息请求(https://github.com/darko1002001/android-rest-client),以防您想要查看有关如何实现所有内容的更多详细信息)。在你的情况下,你需要剥离一些代码并用你自己的代码替换它,但总的想法是一样的。

/**

* @author Darko.Grozdanovski

*/

public class HttpRequestStore {

public static final String TAG = HttpRequestStore.class.getSimpleName();

public static final String KEY_ID ="id";

public static final String IS_SUCCESSFUL ="isSuccessful";

private static final HashMap map = new HashMap();

private final AtomicInteger counter = new AtomicInteger();

private static Class< ? > executorServiceClass = HTTPRequestExecutorService.class;

private final Context context;

private static HttpRequestStore instance;

private HttpRequestStore(final Context context) {

this.context = context;

}

public static HttpRequestStore getInstance(final Context context) {

if (instance == null) {

instance = new HttpRequestStore(context.getApplicationContext());

}

return instance;

}

public static void init(final Class< ? > executorServiceClass) {

HttpRequestStore.executorServiceClass = executorServiceClass;

}

public Integer addRequest(final RequestWrapper block) {

return addRequest(counter.incrementAndGet(), block);

}

public Integer addRequest(final Integer id, final RequestWrapper block) {

map.put(id, block);

return id;

}

public void removeBlock(final Integer id) {

map.remove(id);

}

public RequestWrapper getRequest(final Integer id) {

return map.remove(id);

}

public RequestWrapper getRequest(final Intent intent) {

final Bundle extras = intent.getExtras();

if (extras == null || extras.containsKey(KEY_ID) == false) {

throw new RuntimeException("Intent Must be Filled with ID of the block");

}

final int id = extras.getInt(KEY_ID);

return getRequest(id);

}

public Integer launchServiceIntent(final HttpRequest block) {

return launchServiceIntent(block, null);

}

public Integer launchServiceIntent(final HttpRequest block, RequestOptions options) {

if (executorServiceClass == null) {

throw new RuntimeException("Initialize the Executor service class in a class extending application");

}

if (isServiceAvailable() == false) {

throw new RuntimeException("Declare the" + executorServiceClass.getSimpleName() +" in your manifest");

}

final Intent service = new Intent(context, executorServiceClass);

final RequestWrapper wrapper = new RequestWrapper(block, options);

final Integer requestId = addRequest(wrapper);

service.putExtra(KEY_ID, requestId);

context.startService(service);

return requestId;

}

public boolean isServiceAvailable() {

final PackageManager packageManager = context.getPackageManager();

final Intent intent = new Intent(context, executorServiceClass);

final List resolveInfo = packageManager.queryIntentServices(intent,

PackageManager.MATCH_DEFAULT_ONLY);

if (resolveInfo.size() > 0) {

return true;

}

return false;

}

}

您可以使用Bundle在其他活动中共享变量。如果要在其他活动中传递自己的类对象,请使用Parcelable到您的类

这是一个例子

public class Person implements Parcelable {

private int age;

private String name;

// Setters and Getters

// ....

public int describeContents() {

return 0;

}

public void writeToParcel(Parcel out, int flags) {

out.writeString(name);

out.writeInt(age);

}

public static final Parcelable.Creator CREATOR

= new Parcelable.Creator() {

public Person createFromParcel(Parcel in) {

return new Person(in);

}

public Person[] newArray(int size) {

return new Person[size];

}

};

private Person(Parcel in) {

name = in.readString();

age = in.readInt();

}

}

在包中插入Person对象

Intent i = new Intent();

Bundle b = new Bundle();

b.putParcelable("bob", new Person());

获取Person对象

Intent i = getIntent();

Bundle b = i.getExtras();

Person p = (Person) b.getParcelable("bob");

谢谢你的帮助,这两个答案对我有帮助,但我不能同时接受。

你可以使用intent extras,

Intent intent = new Intent(getBaseContext(), NewActivity.class);

intent.putExtra("DATA_KEY", data);

startActivity(intent)

Intent的文档有更多信息(请参阅标题为"Extras"的部分)。

单身人士将是最好的方法

您可以使用Bundles或共享首选项中的任何一个共享变量或保存变量以供将来使用。

您可以在此处找到共享首选项的示例

您可以在此处找到捆绑包的示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值