Reputation: 32051
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
launches ListActivity
with Intent [EVENTNR=123]
ListActivity
recieves the intent and scrolls to the right position.AnotherActivity
is launched from ListActivity
onStart
of the ListActivity
.ListActivity
scrolls to the same position again (same as in 2.)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?
Upvotes: 0
Views: 353
Reputation: 48272
What if you do getIntent().removeExtra()
to remove your extra data?
On the other hand it might be that putting the info into the Intent
is 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?
Upvotes: 1