-2

I am using adapter where my data stored in the arraylist and added in the adapter,hence i am calling the adapter in ui thread,hence this adapter calls when the user clicks the start button.when the user switch to another activity from same and returning to the same activity my list view got cleared. This is the button function ,while clicking this it will start the service.

Intent start_service = new Intent(MyTrip.this, AppLocationService.class);
startService(start_service);

Hence it call this adapter for every 5min

My adapter is

Adapter = new MyTrip_listview_Adapter(MyTrip.this, location, Date_Array, imagesview, j, context, arrayplaces);
listViewplace.setAdapter(adapter);
listViewplace.setSelection(location.size() - 1);
adapter.notifyDataSetChanged();
Ziem
  • 6,579
  • 8
  • 53
  • 86
hari
  • 11
  • 5

2 Answers2

0

From the limited info that you have provided in your question, it looks like your Activity with the list is being recreated every time the user gets back to it from the other Activity. In that case you need to preserve the state of your Activity. To do that save the data in your adapter in onSaveInstanceState(), then in onCreate() check if you are restoring from a previous state and repopulate the list. Here is a detailed how-to: http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Here is example code (from memory):

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);

    if (savedInstanceState != null) {
        // First restore the content
        ArrayList<String> tmp = savedInstanceState.getParcelableArrayList("ArrayListData");
            myArrayList.addAll(tmp);
        listAdapter.notifyDataSetChanged();
        // Then restore the state
        Parcelable l_state = savedInstanceState.getParcelable("ListState");
        listView.onRestoreInstanceState(l_state);
        } else {
        // Probably initialize listview with default values for a new instance
        }

}

@Override
protected void onSaveInstanceState(Bundle state) {
    super.onSaveInstanceState(state);

    state.putParcelableArrayList("ArrayListData", myArrayList);

    Parcelable mListState = listView.onSaveInstanceState();
    state.putParcelable("ListState", mListState);
}
Dimitar Darazhanski
  • 2,188
  • 20
  • 22
  • can you explain in coding ,how to use the adapter – hari Apr 23 '15 at 06:22
  • http://stackoverflow.com/questions/3014089/maintain-save-restore-scroll-position-when-returning-to-a-listview i have seen this link will it works – hari Apr 23 '15 at 09:46
  • Yes, this will work if you have the ListView in a Fragment: http://stackoverflow.com/a/5688490/625030. But if it is in an Activity, then do the dame as the answer in that post, but replace onPause() with onSaveInstanceState() and onViewCreated(final View view, Bundle savedInstanceState) with onCreate(Bundle savedInstanceState). – Dimitar Darazhanski Apr 23 '15 at 13:29
  • You also need to save the ArrayList of items in the onSaveInstanceState, and repopulated the list in onCreate then restore the state like in the above answer so that it is scrolled to the same item instead of all the way at the top. – Dimitar Darazhanski Apr 23 '15 at 13:53
  • @hari, I updated the answer with code. I did this from memory. I don't have an IDE in front of me right now. Let me know if you need clarification. – Dimitar Darazhanski Apr 23 '15 at 14:05
0

I think I've understood you.

You need that when you come back from another activity, don't lose you data on listview, isn't it?

Ok, you have to handle this:

public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
    case android.R.id.home:
        Intent homeIntent = getIntent();
        startActivity(homeIntent);
}
return (super.onOptionsItemSelected(menuItem));

I can not try this on my IDE because I'm out, but I think that it was this way.

If doesn't work, let me know.

Alejandro Lora
  • 7,203
  • 3
  • 18
  • 34