Main activity¶
Single activity limitation: the whole interaction flow is run into the scope of your chosen activity, so once the flow has begun, starting any other activity will make the flow reset to its initial state when you come back to your chosen activity.
SpeechBar: if you want to use the SpeechBar, don’t forget to make your activity extends the RobotActivity.
GUI handling: the interaction flow displays fragments, so you need to provide a fragment container view to the flow.
The main activity is pretty straight-forward, it just needs to build a Flow, then register
to it in the onCreate()
method, and unregister from it in the onDestroy()
method.
class MainActivity : RobotActivity() {
/////////////////
// INTERACTION //
/////////////////
private val myInteractionBehavior = MyInteractionBehavior()
private val myInteraction = InteractionBuilder()
.withInteractionBehavior(myInteractionBehavior)
.build()
//////////
// FLOW //
//////////
private val myFlow = FlowBuilder(R.id.my_fragment_container_view)
.withInteraction(myInteraction)
.build()
//////////////
// ACTIVITY //
//////////////
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setSpeechBarDisplayStrategy(SpeechBarDisplayStrategy.IMMERSIVE)
setContentView(R.layout.activity_main)
myFlow.register(this)
}
override fun onDestroy() {
myFlow.unregister(this)
super.onDestroy()
}
}
UI-wise, the layout only needs a fragment container view.
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
How to update the GUI while in interaction¶
If you want to update the GUI while in interaction, you have to handle it by yourself.
Here is an example of doing so by passing your interaction’s fragment to your interaction’s behavior, and using the ChildFragmentManager of your interaction’s fragment:
Main activity:
class MainActivity : RobotActivity() {
/////////////////
// INTERACTION //
/////////////////
private val myInteractionFragment = MyInteractionFragment()
private val myInteractionBehavior = MyInteractionBehavior(myInteractionFragment)
private val myInteraction = InteractionBuilder()
.withInteractionBehavior(myInteractionBehavior)
.build()
//////////
// FLOW //
//////////
private val myFlow = FlowBuilder(R.id.my_fragment_container_view)
.withInteraction(myInteraction)
.build()
//////////////
// ACTIVITY //
//////////////
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setSpeechBarDisplayStrategy(SpeechBarDisplayStrategy.IMMERSIVE)
setContentView(R.layout.activity_main)
myFlow.register(this)
}
override fun onDestroy() {
myFlow.unregister(this)
super.onDestroy()
}
}
Interaction behavior:
class MyInteractionBehavior(private val myInteractionFragment: MyInteractionFragment) : InteractionBehavior {
private val mySecondInteractionFragment = MySecondInteractionFragment()
override fun onEnter(qiContext: QiContext, interactionHandler: InteractionHandler) {
myInteractionFragment.displayChildFragment(mySecondInteractionFragment)
}
override fun onExit() {
}
}
Interaction fragment:
class MyInteractionFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_interaction, container, false)
}
fun displayChildFragment(childFragment: Fragment) {
activity?.runOnUiThread {
// Display child fragment
childFragmentManager
.beginTransaction()
.replace(R.id.my_child_fragment_container_view, childFragment)
.commit()
// Show child fragment layout
if (my_child_fragment_container_view.visibility != View.VISIBLE) {
my_child_fragment_container_view.visibility = View.VISIBLE
}
}
}
}
Interaction fragment’s layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyInteractionFragment">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/my_child_fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</androidx.constraintlayout.widget.ConstraintLayout>