简体   繁体   English

如何避免在Android设备上的按钮上重新加载数据

[英]How to avoid data reload on android device back button

I am using 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). 我正在将基于Android选项卡的应用程序与TabGroupActivity一起使用,该应用程序具有一个ListActivity“ A”,该活动可向下钻取到详细信息活动“ D”,当我单击设备后退按钮时,在详细信息活动上它会导航回ListActivtiy“ A”并重新发布数据(ProgressBar在后退按钮上显示进度)。
How can i avoid reloading of data on back buttuon ? 如何避免在后备按钮上重新加载数据?
Any help would be appreciated. 任何帮助,将不胜感激。

也许您可以使用onSaveInstanceState()保存需要保存的内容,然后在onCreate()中恢复您的活动

If you look at the Activity LifeCycle : 如果您查看Activity LifeCycle

When you go to another activity, and press back, it will call finish(). 当您转到另一个活动并按回去时,它将调用finish()。 The first activity was on the onStop() method because it was no longer visible. 第一个活动在onStop()方法上,因为它不再可见。 So, when you come back to the first activity, it will call the method onStart(), and on Stop(). 因此,当您返回第一个活动时,它将调用onStart()和Stop()方法。

Try to overload this method without calling the super method (it might be a very bad idea). 尝试重载此方法而不调用super方法(这可能是一个非常糟糕的主意)。

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. 您还可以尝试记住自己的位置(获取当前选项卡的索引),然后在onResume()方法上将当前选项卡设置为此索引。

You just need an int attribute savedPosition in your listactivity. 您只需要在listactivity中有一个int属性savedPosition。 private int savedPosition = -1; 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);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM