开发者

How to avoid data reload on android device back button

I am using 开发者_StackOverflow社区android tab based application with TabGroupActivity, the application have a ListActivity "A" which drills down to a detail activity "D", on detail activity when i click device back button it navigate back to ListActivtiy "A" and relaods the data (ProgressBar shows progress on back button).

How can i avoid reloading of data on back buttuon ?

Any help would be appreciated.


Perhaps you can use onSaveInstanceState() to save what's needed to be saved and then restore your activity in onCreate()


If you look at the Activity LifeCycle :

When you go to another activity, and press back, it will call finish(). The first activity was on the onStop() method because it was no longer visible. So, when you come back to the first activity, it will call the method onStart(), and on Stop().

Try to overload this method without calling the super method (it might be a very bad idea).

You can also try to remember the position when you was (get the index of the current tab), and on the onResume() method, set the current tab to this index.

You just need an int attribute savedPosition in your listactivity. private int savedPosition = -1;

@Override
protected void onPause() {
  savedPosition = getListView().getFirstVisiblePosition();

  super.onPause();
}



@Override
protected void onResume() {
  super.onResume();
  if(savedPosition != -1) {
    getList().setSelection(savedPosition);
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜