fahmy
fahmy

Reputation: 3672

Implementing a listener in android application class with the event listener in a FragmentActivity

Just started programming and I'm coding my first app. I've been wrecking my brain over this for long enough. I picked this up from tutorial where a listener is implemented in a ListFragment and the event listener is in a FragmentActivity that hosts the ListFragment. Here is how it is in the example (showing only the relevant part):

MyList class:

public class MyList extends ListFragment{

   OnSomeItemSelectedListener mCallback;

   public interface OnSomeItemSelectedListener {
      public void onSomeItemSelectedListener(int position, long id);
    }

   @Override
   public void onAttach(Activity activity) {
      super.onAttach(activity);
      // This makes sure that the container activity has implemented
      // the callback interface. If not, it throws an exception.
      try {
         mCallback = (OnSomeItemSelectedListener) activity;
      } catch (ClassCastException e) {
         throw new ClassCastException(activity.toString()
         + " must implement OnSomeItemSelectedListener");
      }
   }

   @Override
   public void onListItemClick(ListView l, View v, int position, long id) {
      super.onListItemClick(l, v, position, id);
      mCallback.onSomeItemSelectedListener(position, id);
   }
}

MainActivity class:

public class MainActivity extends FragmentActivity implements
        MyList.OnSomeItemSelectedListener{

    @Override
    public void onSomeItemSelectedListener(int position, long id) {
       //some stuff done here when clicked on a item from the listfragment
    }
}

The above works flawlessly.

I'm trying to implement this in an application class to notify the event listener (a FragmentActivity) when an AsyncTask has finished. This is what I did in in the application class:

MyApplication class:

public class MyApplication extends Application {

    OnFetchCompleteListener mCallback;

    public interface OnFetchCompleteListener {
        public void onFetchCompleteListener();
    }

    public synchronized void fetchUpdates() {
        String[] mURLs = getURLs();
        new DownloadDataAsyncTask().execute(mURLs[0], mURLs[1]);
    }

    private class DownloadDataAsyncTask extends
            AsyncTask<String, Void, JSONObject[]> {

        @Override
        protected JSONObject[] doInBackground(String... params) {
            //Some stuff
            return mJSON;
        }

        @Override
        protected void onPostExecute(JSONObject[] result) {
            // Informing listeners on fetchData complete
            mCallback.onFetchCompleteListener();
        }
    }
}

How do I instantiate the mCallback in the application class? Without this,

   mCallback = (OnSomeItemSelectedListener) activity;

I get a null pointer exception from the application class.

Many thanks

Upvotes: 4

Views: 5707

Answers (1)

WU Xudong
WU Xudong

Reputation: 160

I think you can instantiate mCallback from the Activity instead of from the Application.

In your activity's onCreate() or onResume() method, you can write:

MyApplication myApplication = getApplication();
myApplicaton.mCallback = this;

but make sure your activity have implemented OnFetchCompleteListener (I don't why you typed OnSomeItemSelectedListener in the last piece of code.)

Upvotes: 4

Related Questions