简体   繁体   English

从第二个活动更新活动

[英]Update activity from a second activity

I need to update the score in one activity when an action is used in another activity. 当一个动作在另一个动作中使用时,我需要更新一个分数。

In class 1 activity 1 - I have a score textview and this shows the score. 在第1课的活动1中-我有一个得分文本视图,它显示了得分。 The user then presses answer and this takes the user to another activity where they choose an answer from radio buttons. 然后,用户按下答案,这会将用户带到另一个活动,在该活动中,他们从单选按钮中选择一个答案。

In class 2 activity 2 - The user chooses an answer and the score in activity 1 gets updated if correct. 在第2类活动2中-用户选择一个答案,如果正确,则在活动1中的分数会更新。

Is this possible? 这可能吗?

Thanks. 谢谢。

if your using intents to launch you activities then you might want to look into passing the values as extra 如果您使用意图启动活动,那么您可能需要考虑将值作为额外的传递


Edit 编辑

I found a good article about extra (get and set extra) I found a good article about extra (get and set extra) Section 3 should be what your looking for 我找到了一篇关于多余(获取和设置额外内容)的好文章我找到了一篇关于多余(获取和设置额外内容) 的好文章第3部分应该是您想要的

I'd create a class called GameController or something to that effect, and both Activities would interact with a single instance of this class. 我将创建一个名为GameController的类或类似的东西,两个Activity都将与该类的单个实例进行交互。

Or if you want to keep it simple, open Activity2 with startActivityForResult and return the answer back to Activity1. 或者,如果您想保持简单,请使用startActivityForResult打开Activity2,然后将答案返回给Activity1。 That way Activity1 has all the logic for keeping score as well as displaying it. 这样,Activity1拥有保持分数并显示分数的所有逻辑。

Using multiple activities for something like this seems like overkill. 使用多个活动进行类似的操作似乎有点过分。 Why not just have two views in the same activity and just flip between them? 为什么不在同一活动中拥有两个视图并在它们之间切换呢?

You could use the Transition3d sample from the ApiDemos as a starting point. 您可以使用ApiDemos中的Transition3d示例作为起点。

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

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