0

I just published a game on the Play Store and am planning to make it look better and more intuitive, si I'm trying to replace the Android dialogs by mines.

When the user finish a level, I need my custom dialog to show and if the user clicks some buttons, the dialog (which is an activity with a custom theme that make it looks like a dialog) have to return an integer with setResult(customResult, intent) and then finish.

The problem is that when I receive the resultCode in the onActivityResult() of the activity that did startActivityForResult(intentCustomDialog, 0), I call a custom method that restart the level or launch the next one depending on the result (used to know what button the user clicked). This methods should also restarts the Chronometer, but it does nothing !!!!!!!
On the other side when I use the android dialog and I put the restart() call in the onClickListener the chronometer is successfully restarted !?

So what am I missing ? Why the restart() call successfully restart the chronometer with an onClickListener of the android dialogs but not when I call it from the onActivityResult() method ?

Would be very helpful so I could make my own dialogs implementation and use the onActivityResult() or even call a custom onDialogResult() method to separate startActivityForResult() launches of custom dialogs activities and normal activities.

Thanks in advance.

EDIT

In my game class :

...
public static final int RESULT_RESTART = 8;
public static final int RESULT_NEXT = 9;
public static final int RESULT_MENU = 10;
...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (resultCode) {
        case RESULT_MENU:
            back(null);
            break;
        case RESULT_NEXT:
            nextGrid();
            break;
        case RESULT_RESTART:
            restartGrid();
            break;
    }
}

restartGrid() method :

public void restartGrid() {
    ...
    startChrono();
    ...
}

public void startChrono() {
    chrono.setBase(SystemClock.elapsedRealtime());
    chrono.start();
}

Works perfectly from a DialogInterface.OnClickListener().

4

1 回答 1

0

可以让它工作:

public void startChrono() {
    chrono.post(new Runnable() {
        @Override
        public void run() {
            chrono.setBase(SystemClock.elapsedRealtime());
            chrono.start();
        }
    });
}

但我仍然不明白为什么我必须为计时器这样做,因为已着色的视图在没有使用 post 方法的情况下被正确重置......无论如何它都有效:)

于 2012-07-21T15:33:37.467 回答