Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I could do this by passing a reference to the MainActivity to the LoginActivity when calling it, and then in the AsyncTask onPostExecute, call a function in that MainActivity instance to refresh the displayed data.</p> </blockquote> <p>The out-of-the-box way of dealing with activities coming and going is to use <a href="https://developer.android.com/reference/android/content/AsyncTaskLoader.html" rel="nofollow"><code>AsyncTaskLoader</code></a>. This approach implements @ByteMe's suggestion to unregister/register activity instances. Unfortunately, its programmer API is... less than painless. In particular, the API is written for a screen that begins loading immediately when it's opened. Using the <a href="https://developer.android.com/guide/components/loaders.html" rel="nofollow">Loaders</a> API in a screen that begins loading upon user interaction is perfectly possible, but the implementation is a bit awkward.</p> <p>A common alternate approach –still using only core- and support-v4 library classes– is to use an <a href="https://developer.android.com/reference/android/app/IntentService.html" rel="nofollow"><code>IntentService</code></a> to do the background work and the <a href="https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html" rel="nofollow"><code>LocalBroadcastManager</code></a> to deliver the result.</p> <p>If using Open Source libraries is an option, you might want to look at <a href="http://square.github.io/otto/" rel="nofollow">Otto Bus</a> or <a href="https://github.com/Netflix/RxJava" rel="nofollow">RxJava</a> to deliver the "load finished" event.</p> <blockquote> <p>Alternatively, in this instance I could just finish() the MainActivity when I start the LoginActivity, and launch a new MainActivity from the onPostExecute which would display the refreshed data.</p> </blockquote> <p>If I understand correctly, the <code>MainActivity</code> is your launch activity. The first thing it does is check if login is necessary and, if it is, it directs to <code>LoginActivity</code>. If login isn't necessary, it shows the account data and if it is, it waits for <code>LoginActivity</code> to return.</p> <p>This sounds to me like a perfect fit for <a href="http://developer.android.com/reference/android/app/Activity.html#startActivityForResult%28android.content.Intent,%20int%29" rel="nofollow"><code>startActivityForResult(Intent, int)</code></a>. Just have the main activity launch the login activity with a request code. The login activity can call <a href="http://developer.android.com/reference/android/app/Activity.html#setResult%28int,%20android.content.Intent%29" rel="nofollow"><code>setResult(int, Intent)</code></a> with a return code and the authentication data. The main activity can then implement <a href="http://developer.android.com/reference/android/app/Activity.html#onActivityResult%28int,%20int,%20android.content.Intent%29" rel="nofollow"><code>onActivityResult()</code></a> to get the return value from the login activity and do all that needs to be done. </p> <p>As an added benefit, you can centralise all <code>SharedPreferences</code> interaction in the main activity and keep it entirely outside login activity. This makes for lower coupling between the two.</p> <p>An entirely different approach that you could consider is to ditch user authentication entirely and rely on <a href="https://developers.google.com/+/" rel="nofollow">Google+ Sign-in</a> to identify users by their Google+ account. You can do the same through <a href="http://developer.android.com/reference/android/app/Activity.html#onActivityResult%28int,%20int,%20android.content.Intent%29" rel="nofollow">Facebook</a>. On the upside, using either of these (or both) will completely free you from the responsibility of managing authentication and credentials in a secure way. On the downside, there's vendor lock-in and it may scare off some users.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload