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:
CallingActivitylaunchesListActivitywith Intent[EVENTNR=123]ListActivityrecieves the intent and scrolls to the right position.- User continues to read and scrolls further down.
AnotherActivityis launched fromListActivity- The User presses the Back-Button
- The old intent (from 1.) is still present and gets passed again in
onStartof theListActivity. ListActivityscrolls 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?
What if you do
getIntent().removeExtra()to remove your extra data?On the other hand it might be that putting the info into the
Intentis an unnesessary complication as you don’t really need an Intent for that.Can it be a plain java object to store that info with API to set and reset it?