Problem:
I’ve written State Machine for my android application. It is separate class, extension of Android 4.0.3 StateMachine. And I want this SM could switch activities.
Is it possible to implement?
I mean that such thing as
startActivity(new Intent(CurrentActivity.this, NextActivity.class))was called not from current activity but inside my state machine.
Thanks and sorry for my bad English.
My solution:
(special thanks to @Jan-Henk)
Inside my CurrentActivity call
stateMachine.sendMessage(SM.MSG_SWITCH_ACTIVITY, CurrentActivity.this);
and inside my State put next code:
@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;
}
You can call
startActivityfrom a Context object, see http://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent).