by

Creating a back stack for MVVMCross application

When I was working on the STAmina app, I was faced a pretty interesting task – manually create an android back stack that allows to navigate back by history. Let’s take a look how it can be implemented.

Desired behavior

Let’s take another look at the desired behavior of the app:

  1. A notification message is shown to a user;
  2. User clicks on the notification and is navigated to the activity A3;
  3. User clicks on the back button and is navigated to the activity A2 and then A1.

Additionally, the view models behind A1, A2 and A3 expect init parameters and won’t work without them.

OK, let’s start with TaskStackBuilder.

TaskStackBuilder

The TaskStackBuilder class will construct the synthetic back stack that satisfies our needs and will allow to obtain a PendingIntent for launching the task constructed by this builder.

Notice that we add to the builder the most deepest Activity first. Now we’ll use that pendingIntent to create a notification.

Now let’s take a step back look how to construct activity intents (intentA1, intentA2, intentA3) to pass parameters to view models.

MVVMCross

Luckily for us, mvvmcross offers an extension method that does exactly what we want to – constructs an intent that references a given model and allows to pass parameters to it.

That’s it! I’ve created a sample project that demonstrates the above. Grab it on the GitHub.

Screenshot_20151210-120104

Conclusion

With that simple technique we can not only construct back stacks for our needs, but also navigate users from non-mvvmcross-views to mvvmcross-views that require initiation parameters.

Write a Comment

Comment