ui: Properly reload accounts' data

Refs #44887
diff --git a/src/org/sflphone/fragments/AccountsManagementFragment.java b/src/org/sflphone/fragments/AccountsManagementFragment.java
index 206770e..aa38610 100644
--- a/src/org/sflphone/fragments/AccountsManagementFragment.java
+++ b/src/org/sflphone/fragments/AccountsManagementFragment.java
@@ -97,6 +97,7 @@
             return null;
         }
     };
+    private AccountsLoader accountsLoader;
 
     public interface Callbacks {
 
@@ -131,7 +132,7 @@
 
         mShortAnimationDuration = getResources().getInteger(android.R.integer.config_mediumAnimTime);
         Log.i(TAG, "anim time: " + mShortAnimationDuration);
-
+        getLoaderManager().initLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
     }
 
     @Override
@@ -177,8 +178,7 @@
 
     public void onResume() {
         super.onResume();
-
-        getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
+        accountsLoader.onContentChanged();
         getActivity().getActionBar().setTitle(R.string.menu_item_accounts);
     }
 
@@ -215,8 +215,7 @@
 
     @Override
     public void accountsChanged() {
-        if (getActivity() != null)
-            getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
+        accountsLoader.onContentChanged();
     }
 
     @Override
@@ -227,9 +226,7 @@
     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
-
-        getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
-
+        accountsLoader.onContentChanged();
     }
 
     /**
@@ -396,9 +393,8 @@
 
     @Override
     public AsyncTaskLoader<Bundle> onCreateLoader(int arg0, Bundle arg1) {
-        AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService());
-        l.forceLoad();
-        return l;
+        accountsLoader = new AccountsLoader(getActivity(), mCallbacks.getService());
+        return accountsLoader;
     }
 
     @Override
diff --git a/src/org/sflphone/loaders/AccountsLoader.java b/src/org/sflphone/loaders/AccountsLoader.java
index 81397db..0dc2e26 100644
--- a/src/org/sflphone/loaders/AccountsLoader.java
+++ b/src/org/sflphone/loaders/AccountsLoader.java
@@ -49,12 +49,17 @@
     public static final String ACCOUNTS = "accounts";
     public static final String ACCOUNT_IP2IP = "IP2IP";
     ISipService service;
+    Bundle mData;
 
     public AccountsLoader(Context context, ISipService ref) {
         super(context);
         service = ref;
     }
 
+    /****************************************************/
+    /** (1) A task that performs the asynchronous load **/
+    /****************************************************/
+
     @SuppressWarnings("unchecked")
     // Hashmap runtime cast
     @Override
@@ -95,4 +100,92 @@
         return result;
     }
 
+
+    /********************************************************/
+    /** (2) Deliver the results to the registered listener **/
+    /********************************************************/
+
+    @Override
+    public void deliverResult(Bundle data) {
+        if (isReset()) {
+            // The Loader has been reset; ignore the result and invalidate the data.
+            releaseResources(data);
+            return;
+        }
+
+        // Hold a reference to the old data so it doesn't get garbage collected.
+        // We must protect it until the new data has been delivered.
+        Bundle oldData = mData;
+        mData = data;
+
+        if (isStarted()) {
+            // If the Loader is in a started state, deliver the results to the
+            // client. The superclass method does this for us.
+            super.deliverResult(data);
+        }
+
+        // Invalidate the old data as we don't need it any more.
+        if (oldData != null && oldData != data) {
+            releaseResources(oldData);
+        }
+    }
+
+    /*********************************************************/
+    /** (3) Implement the Loader’s state-dependent behavior **/
+    /*********************************************************/
+
+    @Override
+    protected void onStartLoading() {
+        if (mData != null) {
+            // Deliver any previously loaded data immediately.
+            deliverResult(mData);
+        }
+
+        if (takeContentChanged() || mData == null) {
+            // When the observer detects a change, it should call onContentChanged()
+            // on the Loader, which will cause the next call to takeContentChanged()
+            // to return true. If this is ever the case (or if the current data is
+            // null), we force a new load.
+            forceLoad();
+        }
+    }
+
+    @Override
+    protected void onStopLoading() {
+        // The Loader is in a stopped state, so we should attempt to cancel the
+        // current load (if there is one).
+        cancelLoad();
+
+        // Note that we leave the observer as is. Loaders in a stopped state
+        // should still monitor the data source for changes so that the Loader
+        // will know to force a new load if it is ever started again.
+    }
+
+    @Override
+    protected void onReset() {
+        // Ensure the loader has been stopped.
+        onStopLoading();
+
+        // At this point we can release the resources associated with 'mData'.
+        if (mData != null) {
+            releaseResources(mData);
+            mData = null;
+        }
+    }
+
+    @Override
+    public void onCanceled(Bundle data) {
+        // Attempt to cancel the current asynchronous load.
+        super.onCanceled(data);
+
+        // The load has been canceled, so we should release the resources
+        // associated with 'data'.
+        releaseResources(data);
+    }
+
+    private void releaseResources(Bundle data) {
+        // For a simple List, there is nothing to do. For something like a Cursor, we
+        // would close it in this method. All resources associated with the Loader
+        // should be released here.
+    }
 }