0

问题:

我已经为我的 android 应用程序编写了状态机。它是独立的类,Android 4.0.3 StateMachine 的扩展。我希望这个SM可以切换活动。
是否可以实施?
我的意思是这样的事情

startActivity(new Intent(CurrentActivity.this, NextActivity.class))

不是从当前活动中调用的,而是在我的状态机中调用的。
感谢并为我糟糕的英语感到抱歉。



我的解决方案:

(特别感谢@Jan-Henk)

在我的 CurrentActivity 调用
stateMachine.sendMessage(SM.MSG_SWITCH_ACTIVITY, CurrentActivity.this);
和我的 State 中放置下一个代码:

@Override
public void exit()
{
    final Intent intent = new Intent(context, NextActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // necessary to avoid exceptions
    context.startActivity(intent);
}

@Override
public boolean processMessage(final Message message)
{
    boolean returnedValue;
    switch(message.what)
    {
        case MSG_SWITCH_ACTIVITY:
            //sendMessage(obtainMessage(MSG_SWITCH_ACTIVITY));
            context = (Context) message.obj; // context - it's a field of my state machine
            transitionTo(nextActivity);
            returnedValue = HANDLED;
        break;
        default:
            returnedValue = NOT_HANDLED;
        break;
    }           
    return returnedValue;
}
4

2 回答 2

1

您可以startActivity从 Context 对象调用,请参阅http://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent )。

于 2012-07-03T22:04:49.893 回答
0

要调用下一个活动,您可以像 Jan-henk 提到的那样简单地设置一个新意图并调用 start 活动

Intent intent = new Intent(this, yoursecondclass.class);
startActivity(intent);
于 2012-07-03T22:48:00.507 回答