Permalink
Browse files

Wait until onResume() is complete for OnScrollListener.

Retain posts when switching tabs.
  • Loading branch information...
1 parent 1cb4bdf commit b3a02e4e647e88a33b6511917aa7958a7172d293 @thomaslee committed Sep 3, 2012
@@ -58,6 +58,8 @@
private static final String TAB_MENTIONS_TITLE = "Mentions";
+ private boolean ready = false;
+
private String accessToken;
private ListView myStreamListView;
private ListView mentionsListView;
@@ -110,11 +112,11 @@ public void onTabChanged(String tabId) {
stream = Stream.MY_STREAM;
myStreamListView = (ListView) findViewById(R.id.my_stream_list_view);
- myStreamListView.setAdapter(myStreamPostsAdapter);
+ myStreamListView.setAdapter(myStreamPostsAdapter);
myStreamListView.setOnScrollListener(this);
myStreamListView.setOnItemClickListener(this);
myStreamListView.setOnItemLongClickListener(this);
-
+
mentionsListView = (ListView) findViewById(R.id.mentions_list_view);
mentionsListView.setAdapter(mentionsPostsAdapter);
mentionsListView.setOnScrollListener(this);
@@ -140,6 +142,8 @@ public boolean onCreateOptionsMenu(Menu menu) {
*/
@Override
protected void onResume() {
+ super.onResume();
+
final SharedPreferences prefs = getSharedPreferences(Pref.FILENAME, 0);
accessToken = prefs.getString(Pref.PREF_ACCESS_TOKEN, null);
@@ -152,7 +156,7 @@ protected void onResume() {
requestAuthorization();
}
- super.onResume();
+ ready = true;
}
/*
@@ -161,6 +165,11 @@ protected void onResume() {
*/
@Override
protected void onPause() {
+ ready = false;
+
+ postsAdapter.setPosts(new ArrayList<Post>());
+ postsAdapter.notifyDataSetChanged();
+
stopPollThread();
accessToken = null;
@@ -189,12 +198,14 @@ public boolean onMenuItemClick(MenuItem item) {
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
- final boolean needMore = (firstVisibleItem + visibleItemCount >= totalItemCount) &&
- !postsAdapter.getEndOfTime() &&
- postsAdapter.getPosts().size() < MAX_POSTS;
-
- if (!postsAdapter.isLoading() && needMore) {
- new PostsBeforeTask(stream, postsAdapter, new AppNetClient(accessToken), postsAdapter.getOldestPostId()).execute();
+ if (ready) {
+ final boolean needMore = (firstVisibleItem + visibleItemCount >= totalItemCount) &&
+ !postsAdapter.getEndOfTime() &&
+ postsAdapter.getPosts().size() < MAX_POSTS;
+
+ if (!postsAdapter.isLoading() && needMore) {
+ new PostsBeforeTask(stream, postsAdapter, new AppNetClient(accessToken), postsAdapter.getOldestPostId()).execute();
+ }
}
}
@@ -211,9 +222,6 @@ private void startPollThread() {
}
private void stopPollThread() {
- postsAdapter.setPosts(new ArrayList<Post>());
- postsAdapter.notifyDataSetChanged();
-
if (pollThread != null) {
pollThread.interrupt();
try {
@@ -61,7 +61,9 @@ protected final void onPreExecute() {
try {
final HashMap<Stream, List<Post>> map = new HashMap<Stream, List<Post>>();
for (Map.Entry<Stream, PostsAdapter> entry : streamAdapters.entrySet()) {
- map.put(entry.getKey(), getPosts(entry.getKey(), batchSize));
+ final Stream stream = entry.getKey();
+ final List<Post> posts = getPosts(stream, batchSize);
+ map.put(stream, posts);
}
return map;
}
@@ -26,7 +26,7 @@ public PostsSinceTask(final Map<Stream, PostsAdapter> streamAdapters, final AppN
@Override
protected List<Post> getPosts(final Stream stream, final int batchSize) throws AppNetException {
- return client.getPostsSince(stream, id, batchSize);
+ return client.getPostsSince(stream, id, batchSize);
}
@Override

0 comments on commit b3a02e4

Please sign in to comment.