Activity doesn't refresh after associated Broadcast Receiver's onReceive gets called.
<p>I have an Updater Service which runs in background and keeps broadcasting intents in case an update is found. I have an activity (Timeline Activity) which displays some data based on the updates fetched by the Updater Service. I have another Broadcast Receiver associated to the Timeline Activity which listens to the intent broadcasted by the Updater Service. Now, whenever a new update is fetched by the Updater Service it properly broadcasts the intent and is being received by the Receiver associated with the Timeline Activity too. The problem is the data on the UI isn't getting refreshed after the broadcast is received. The SimpleCursorAdapter responsible for updating isn't doing anything after <code>notifyDataSetChanged()</code>  is getting called in <code>onReceive()</code> of the receiver.</p>

<p>Following is the code for TimelineActivity.java</p>

<pre><code>public class TimelineActivity extends BaseActivity {

public static final String TAG = "TimelineActivity";
private Cursor cursor;
private ListView listTimeline;
//Adapter for connecting ListView and the cursor containing info from database
private SimpleCursorAdapter timelineAdapter;  
static final String[] FROM = { DBHelper.C_CREATED_AT, DBHelper.C_USER,
    DBHelper.C_TEXT }; 
static final int[] TO = { R.id.textCreatedAt, R.id.textUser, R.id.textText };  

YambaApplication yamba;
private TimelineReceiver timelineReceiver = null; 
private IntentFilter intentFilter = new IntentFilter("com.pack.android.yamba.NEW_STATUS"); 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.timeline);

    //Fetch the views
    listTimeline = (ListView) findViewById(R.id.listTimeline);

    yamba = (YambaApplication) getApplication();
    if(yamba.getSharedPreferences().getString("username", null) == null)
    {
        startActivity(new Intent(this, PrefsActivity.class)); // 
        Toast.makeText(this, R.string.msgSetupPrefs, Toast.LENGTH_LONG).show();
    }

    // Check if preferences have been set
    if (yamba.getSharedPreferences().getString("username", null) == null) { // 
      startActivity(new Intent(this, PrefsActivity.class));
      Toast.makeText(this, R.string.msgSetupPrefs, Toast.LENGTH_LONG).show();
    }

    timelineReceiver = new TimelineReceiver();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "On Destroy of Timeline Activity called !");
    //close the database
    yamba.getStatusData().close();
}

//TODO : check for onWindowFocusChanged(boolean hasFocus) method for detailed test
@Override
protected void onResume() {
    super.onResume();
    Log.d(TAG, "On Resume of Timeline Activity called !");
    setUpList();
    Log.d(TAG, "Setting up list completed !");
    registerReceiver(timelineReceiver, intentFilter);
}

@Override
protected void onPause() {
    super.onPause();
    Log.d(TAG, "On Pause of Timeline Activity called !");
    Log.d(TAG, "In onPause timelineAdapter.getCursor().close() "+timelineAdapter.getCursor());
    timelineAdapter.getCursor().close();
    Log.d(TAG, "Closed timelineAdapter.getCursor");
    Log.d(TAG, "In onPause cursor = "+cursor);
    cursor.close();
    Log.d(TAG, "Closed cursor");
    unregisterReceiver(timelineReceiver);
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    Log.d(TAG, "Does Timeline Activity has focus ? "+ hasFocus);
}

@Override
protected void onStop() {
    super.onStop();
    Log.d(TAG, "OnStop of Timeline Activity called !");
    Log.d(TAG, "In onStop timelineAdapter.getCursor().close() "+timelineAdapter.getCursor());
    timelineAdapter.getCursor().close();
    Log.d(TAG, "Closed timelineAdapter.getCursor");
    Log.d(TAG, "In onStop cursor = "+cursor);
    cursor.close();
    Log.d(TAG, "Closed cursor");
}

private void setUpList() {

    // Get the data from the database
    // TODO : verify for honeycomb which will use loaderManagers for managing lifecycle of a cursor
    Log.d(TAG, "Setting up the list");
    cursor = yamba.getStatusData().getStatusUpdates();
    Log.d(TAG, "cursor is "+ cursor);
    startManagingCursor(cursor);

    // Setup Adapter
    timelineAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, FROM, TO);
    Log.d(TAG, "Timeline adapter is "+ timelineAdapter);
    Log.d(TAG, "Count of data = " + timelineAdapter.getCount());
    Log.d(TAG, "Is cursor empty ? " + timelineAdapter.isEmpty());
    timelineAdapter.setViewBinder(VIEW_BINDER); // 
    listTimeline.setAdapter(timelineAdapter);
}

static final ViewBinder VIEW_BINDER = new ViewBinder() {

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        if(view.getId() != R.id.textCreatedAt)
            return false;

        // Update the created at text to relative time
        long timeStamp = cursor.getLong(columnIndex);
        CharSequence relativetime = DateUtils.getRelativeTimeSpanString(timeStamp);
        ((TextView)view).setText(relativetime);
        return true;
    }
};

class TimelineReceiver extends BroadcastReceiver 
{
    private static final String TAG = "TimelineReceiver";
    @Override
    public void onReceive(Context context, Intent intent) 
    { 
        Log.d(TAG, "Just before requery, cursor is " + cursor);
        yamba.getStatusData().getCursor().requery(); //
        Log.d(TAG, "Just after requery");
        timelineAdapter.notifyDataSetChanged(); //
        onResume();
        Log.d(TAG, "on Receive of "+TAG);
    }
}
</code></pre>

<p>}</p>

<p>The following code in UpdaterService is broadcasting intents :</p>

<pre><code>Intent intent = new Intent("com.pack.android.yamba.NEW_STATUS");
                  String NEW_STATUS_EXTRA_COUNT = "com.pack.android.yamba.EXTRA_COUNT";
                  intent.putExtra(NEW_STATUS_EXTRA_COUNT, newUpdates);
                  updaterService.sendBroadcast(intent);
</code></pre>

<p>Can anyone suggest something ?</p>

<p>Thanks,
Adithya.</p>
