title: Session Management with Xamarin.Forms (Android)
body: <p>First of all, I'm not sure if I am even going about this the best way in terms of design, but I'm a little new to Android and very new to Xamarin.Forms. My setup is a series of <code>ContentPage</code>s in a Xamarin.Forms PCL that all deal with information from a REST service. This service, upon logging in, grants a <code>WorkToken</code> that the application then holds onto in order to perform further operations. I'm storing this in an <code>ISessionManager</code>. </p>
  
  <p>My problem is a workflow issue. I have a <code>LoginPage</code> that I want to present to the user if their session has ended/faulted/whatever; basically if they aren't logged in and try to do anything, I want to push this page on the stack and force them to attempt to log in and if they provide valid credentials let them continue where they left off. All this is taking place, for the most part, in the PCL, but I've created an Android implementation of the <code>ISessionManager</code> that is queried on each <code>ContentPage</code>'s overridden <code>OnAppearing</code> method. Each page calls <code>ISessionManager</code>'s <code>CheckLogin</code> which will do just that, and if they aren't logged in, it will create an <code>Intent</code> and start the <code>LoginActivity</code> through that. <code>LoginActivity</code> essentially just wraps around the PCL's <code>LoginPage</code>.</p>
  
  <p>The problem I am running into is that, doing it this way, the <code>LoginActivity</code> is run asynchronously on top of whatever page called it. This calling page is in a broken state, however, because it has not initialized properly through the REST service yet. I imagine there are two possible solutions. Either run the <code>Intent</code> synchronously (which I don't think is possible) or rethink my design. Here's the <code>Intent</code> setup:</p>
  
  <pre><code>var intent = new Intent(_context, typeof(LoginActivity));
  intent.AddFlags(ActivityFlags.NewTask);
  _context.StartActivity(intent);
  </code></pre>
  
  <p>I've checked several Xamarin forums for related posts on this, but have not found much beyond basic navigation workflows in Xamarin.Forms.</p>
  
  <p>Edit: I've found something analogous to what I'm trying to do here: <a href="https://stackoverflow.com/questions/12468610/how-to-pass-variables-from-a-new-intent-back-to-the-class-that-created-it-in-and">How to pass variables from a new intent back to the class that created it in android</a>, but within the context of Xamarin.Forms. The <code>startActivityForResult</code> would solve my problem if it existed in <code>Forms.Context</code>, but there doesn't appear to be an equivalent.</p>