Question

I have a FragmentActivity that has several MenuItem's. When I press one of these MenuItems, I start another FragmentActivity which is just a modal window. There is some webservice operations that happen when this activity is started to show a ListView. While this is happening in an AsyncTask, I want to animate the MenuItem with a spinner. I have this working like so, pseudo code ahead:

public class ActivityOne extends FragmentActivity {

   _menuItem.getActionView().setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v) { 
       // this changes the menu item to show an animated spinner
       _menuItem.setActionView(R.layout.actionbar_indeterminate_progress);
       Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
       startActivityForResult(intent, RESULT_SETTINGS);
     }
   });
}

Now, inside of ActivityTwo, I want to be able to change the MenuItem back to the non-spinner view when the long running task is complete...

public class ActivityTwo extends FragmentActivity {

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        new AsyncTask<Void, Void, Cursor>() {

           @Override
           protected Cursor doInBackground(Void... params) {
               // do some stuff that takes a bit
           }

           @Override
           protected void onPostExecute(Cursor cursor) {
              // THIS IS WHERE I NEED TO CHANGE THE MENU ITEM BACK
           }
        }
    }
}

Any advice is appreciated.

Was it helpful?

Solution

After much research and a few conversations with colleagues, I learned that I could use a local BroadcastReceiver to achieve the communication between activities.

public class ActivityOne extends FragmentActivity {

   public static final String MENU_ITEM_EVENT = "com.myApp.MenuItemEvent";

   protected void onCreate(Bundle savedInstanceState) {

      _updateMenuItemReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              _menuItem.getActionView().setVisibility(0);
          }
      };

      _menuItem.getActionView().setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) { 
           // this changes the menu item to show an animated spinner
           _menuItem.setActionView(R.layout.actionbar_indeterminate_progress);
           Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
           startActivityForResult(intent, RESULT_SETTINGS);
         }
      });
   }

   @Override
   protected void onResume() {
   if (_updateMenuItemReceiver != null) {
      LocalBroadcastManager.getInstance(this).unregisterReceiver(_updateMenuItemReceiver);       
   }
}

And the updated ActivityTwo

public class ActivityTwo extends FragmentActivity {

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        new AsyncTask<Void, Void, Cursor>() {

           @Override
           protected Cursor doInBackground(Void... params) {
               // do some stuff that takes a bit
           }

           @Override
           protected void onPostExecute(Cursor cursor) {
              Intent intent = new Intent(ActivityOne.MENU_ITEM_EVENT);
              LocalBroadcastManager.getInstance(MyApplication.getContext()).sendBroadcast(intent);
           }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top