Note that there are some explanatory texts on larger screens.

plurals
  1. POupdate ListView after onStop()
    primarykey
    data
    text
    <p>I have a <code>ListView</code> in a <code>fragment</code> which I can add to from a <code>BroadcastReceiver</code>. However, when the app is removed from the "recents" panel (swipe the thumbnail of the app away - NOT choosing Force Stop in Settings) the <code>BroadcastReceiver</code> still runs (as it is supposed to do do when an app is removed from recents) but I get a Force Close dialog when it tries to update the <code>ListView</code>.</p> <p>What I have gathered about what happens when removing an app from recents is that it does not kill the app, it just stops all the activiites. This means that the <code>BroadcastReceivers</code> and <code>Services</code> keep running. This is where my problem lies - I try to update the ListView in an <code>Activity</code> which has been stopped.</p> <p>EDIT: I think that removing from recents causes onStop() to be called. </p> <p>Do I need to create a service that update the ListView and keeps the activity running? Will it make any difference?</p> <p>What I am trying to do is similar to say an SMS app. In an SMS app, a Broadcast is received and the ListView with the messages is updated to show the new message. </p> <p><strong>EDIT:</strong> Added some code</p> <p>This is the <code>Fragment</code> which contains the <code>ListView</code>:</p> <pre><code>public class HistoryFragment extends FragmentBase implements OnItemAddedHandler { ListView lv; HistoryAdapter simpleAdpt; int mPosition; int index; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View histView = inflater.inflate(R.layout.history_fragment, container, false); setHasOptionsMenu(true); ListView lv = (ListView) histView.findViewById(R.id.h_listView); simpleAdpt = new HistoryAdapter(); lv.setAdapter(simpleAdpt); return histView; } private class HistoryAdapter extends BaseAdapter { private List&lt;Map&lt;String, Object&gt;&gt; mPlanetsList; public HistoryAdapter() { mPlanetsList = DataModel.getInstance().getPlanetList(); } @Override public int getCount() { return mPlanetsList.size(); } @Override public Object getItem(int position) { return mPlanetsList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (null == convertView) { convertView = LayoutInflater.from(getActivity()).inflate( R.layout.history_item, null); Log.i("convertView", "was null"); } TextView tv_title = (TextView) convertView .findViewById(R.id.hi_tv_title); // This is part of the layout of each item HashMap&lt;String, String&gt; itemDataHashMap = (HashMap&lt;String, String&gt;) getItem(position); tv_title.setText(itemDataHashMap.get("planet")); return convertView; } } @Override public void onItemAdded(Object data) { simpleAdpt.notifyDataSetChanged(); } @Override public void onItemRemove(int postion) { simpleAdpt.notifyDataSetChanged(); } } </code></pre> <p>This is the <code>BroadcastReceiver</code> that I am trying to use to add items to the <code>ListView</code>. It is fired using an <code>AlarmManager</code>. This means that there is time for the user to remove the app from the recents panel before the item is added to the <code>ListView</code>:</p> <pre><code>public class ReminderBroadcastReceiver extends BroadcastReceiver { // This is declared in the manifest @Override public void onReceive(Context context, Intent intent) { String title = "title"; DataModel.getInstance() .addItem(title); // Add to History } } </code></pre> <p>In <code>DataModel</code> there is:</p> <pre><code> public static DataModel getInstance() { if (null == instance) { Log.i("getInstance", "null"); instance = new DataModel(); } return instance; } private DataModel() { initList(); } private void initList() { mHistoryList = History.getList(); for (int i = 0; i &lt; mHistoryList.size(); i++) { mPlanetsList.add(mHistoryList.get(i).createPlanet()); } } public void addItem(String title) { History history = new History(); history.getDataHashMap().put("planet", title); history.addToHistoryDB(); // This just adds to a Database mHistoryList.add(0, history); // Help keep the orders the same mPlanetsList.add(0, history.createPlanet()); if (null != mOnItemAddHandler) { mOnItemAddHandler.onItemAdded(title); } } </code></pre> <p>If any more code is needed, please say</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload