开发者

Modify a count through another activity

I made an activity that increases a counter by one if the answer is correct, and decreases a counter by one if the answer is wrong. I have two different counters, and I modify them alternately.

So far everything works, but I would like to add to it so that if an answer is correct a new activity is opened. Here the user will decide whether the counter will be increased or not.

I have spent one day on it so far, but have n开发者_开发问答ot been successful.


Because you need only two counters, I will propose one very simple sollution, not "fancy" or "object oriented", just simple and understandable.

Use two static variables in the class of your main activity, let say:

public static int player1Score, player2Score;

Set them before starting other activities which need them:

player1Score = player1Info.playerScore;
player2Score = player2Info.playerScore;
startActivityForResult(i, 1);

Use them directly in your other activities. Write back their values in onActivityResult():

player1Info.playerScore = player1Score;
player2Info.playerScore = player2Score;

Before writing them back you can also check if their values are changed:

if ( player1Info.playerScore != player1Score )
    ...

Thats all. Forget all complicated sentences like "observer design paradigm", they can't give you nothing here but lost time.

Edit:

If by some reason you don't want to use static variables, you can subclass Application (android.app.Application) and declare normal variables there. Then in every activity you can get your application object through getApplication() and access your common variables there.


I am looking at the question on my phone so haven't been able to scroll through the code so maybe I don't fully understand your problem.

My suggestion is to add an observer to your new activity, the observer can have two methods {onIncremenet() and onDecremenet()} which would be invoked depending upon user action. In the implementation of your observer you can hold a reference to both your counter and depending upon the user action increment or decrement it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜