Embedding the Poll in your Mobile App
Navigation
Navigation is a core part of your app, so our SDK aims to be flexible and allows you to control the navigation.
You will want to consider the following when planning navigation:
- Opening the poll after creating it
- Navigation to the Details screen of a Poll Option
- Opening a poll from a deeplink (for voters)
- Closing/Dismissing the poll
PollFragment
The PollFragment handles all of the functionality of viewing and interacting the a poll. The fragment can be used to display the poll after creating it, or for displaying a poll when a user opens it or deep links into it.
In the example below, we are creating an instance of the fragment and presenting it.
- Kotlin
val pollFragment = PollFragment.newInstance( pollUrl = pollUrl, flow = OpenPollFlow.createPoll ) supportFragmentManager.beginTransaction() .replace(R.id.container, pollFragment, "TAG_POLL") .commit()
Listener
The PollFragment.PollFragmentListener
can be used to receive callbacks about what is happening in the poll.
- Kotlin
class MainActivity : AppCompatActivity(), PollFragment.PollFragmentListener { /// ... override fun didPressViewDetails(option: PollOptionResponse) { // TODO: navigate to the details screen for the option } override fun didReceiveAnalyticsEvent(event: AnalyticsEvent) { // TODO: forward analytics to your system (e.g. Google Analytics) }}
Putting it all together
At this point we have learned how to:
- Configure the SDK
- Create a poll by mapping our data into the poll fields
- Render a poll using the PollViewController
- Handle navigating to the Details Screen of an Option
Let's see it all together below
- Kotlin
// configure Polls Platform SDK val config = Config( apiKey = "YOUR_API_KEY", domainConfig = DomainConfig.Subdomain(subdomain = "YOUR_SUBDOMAIN"), environment = Environment.production ) PollsPlatform.setConfig(config = config) // create a poll val poll = Poll( ownerId = ownerId, settings = settings, title = title, options = options, ) val res = PollsPlatform.createPoll(poll = poll) val pollFragment = PollFragment.newInstance( pollUrl = pollUrl, flow = OpenPollFlow.createPoll ) supportFragmentManager.beginTransaction() .replace(R.id.container, pollFragment, "TAG_POLL") .commit() // deep link into a poll fun handleDeepLink(uri: URI) { if (isPollUrl(uri = uri)) { // this is a poll url val pollFragment = PollFragment.newInstance( pollUrl = pollUrl, flow = OpenPollFlow.openPoll // note we use .openPoll here ) supportFragmentManager.beginTransaction() .replace(R.id.container, pollFragment, "TAG_POLL") .commit() } }