I got a complicated problem involving a ListView and some Intents on Android:
I have a ListActivity
which contains many items. From another Activity, lets call it CallingActivity
, I want to launch this ListActivity
and scroll to a specific item. In the ListActivity
some other Activites can be launched, lets call them AnotherActivity
.
My current implementation is to store the item to which the ListActivity should scoll in the intent and then use the following code in the onStart()
method.
if (getIntent().getExtras() != null && getIntent().getExtras().containsKey(EXTRA_EVENTNR)) {
int p = getIntent().getExtras().getInt(EXTRA_EVENTNR);
getListView().clearFocus();
getListView().post(new Runnable() {
@Override
public void run() {
getListView().setSelection(p);
}
});
}
This solution has a huge drawback:
Suppose the following call-sequence:
CallingActivity
launchesListActivity
with Intent[EVENTNR=123]
ListActivity
recieves the intent and scrolls to the right position.- User continues to read and scrolls further down.
AnotherActivity
is launched fromListActivity
- The User presses the Back-Button
- The old intent (from 1.) is still present and gets passed again in
onStart
of theListActivity
. ListActivity
scrolls to the same position again (same as in 2.)- Scrolling of the User from 3. is lost
I think a Intent is the wrong solution for carrying the scroll-information. Intents are more for the contents of a activity and not for its display.
Is there another solution for passing the scrolling information which only gets applied once? Did I miss something?