Search code examples
androidandroid-fragmentsandroid-activityandroid-loadermanagerandroid-loader

Start Activity in onLoadFinished method


I have DialogFragment and a Loader.

Dialog prompts user to logout from Application.

When user chooses to logout I fire Loader which clears the preferences and database and then I finish current Activity.

After Loader is finished I would like to start a new Activity with login screen, but I encounter two problems:

  1. getActivity() method returns null
  2. I'm not able to run new Activity because I'm out of Activity context

Do You have any solutions for this?


Solution

  • It doesn't sound like your situation warrants the use of a Loader or the LoaderManager at all.

    1. Use an AsyncTask to clear the preferences and database (perhaps showing a ProgressDialog of some sorts if this operation is time-consuming).

    2. When the task is complete, onPostExecute is called. Write a callback method from the fragment to the activity which will first call startActivity(...) and then call finish().