简体   繁体   English

在两个活动之间切换而不会丢失数据

[英]switch between two activities without data lost

Just want to switch between activities and doesn't want to loose information on any activities.For example: 只想在活动之间进行切换,并且不想丢失任何活动的信息,例如:

I have one activity ie activity_one which has one listview , load thousands of data from webservice. 我有一个活动,即具有一个listview的activity_one,从webservice加载了数千个数据。 now I have another activity ie activity_two which has one listview , load thousands of data from webservice. 现在我有另一个活动,即具有一个listview的activity_two,从webservice加载了数千个数据。

when i move from activity_one to activity_two then activity_one data lost and vice versa. 当我从activity_one移到activity_two时,activity_one的数据丢失了,反之亦然。

when its loaded once then don't want to do it again while application is running. 当它加载一次,然后不想在应用程序运行时再次做。

You can retain data between activities using onSaveInstanceState and onRestoreInstanceState. 您可以使用onSaveInstanceState和onRestoreInstanceState在活动之间保留数据。

here is an example demo 这是一个示例演示

float[] localFloatArray = {3.14f, 2.718f, 0.577f};
String localUserName = "Euler";

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //Save the relevant information
    outState.putString("name", localUserName);
    outState.putFloatArray("array", localFloatArray);
}


@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
     //Restore the relevant information
    localUserName = savedInstanceState.getString("name");
    localFloatArray = savedInstanceState.getFloatArray("array");
}

The real thing is how do you move between those two activities. 真正的事情是您如何在这两个活动之间移动。 Maybe you are finishing them when you switch. 切换时可能正在整理它们。 If that's the case you can save the info in a static ArrayList and on the onCreate method you can check if the length of it is 0, if it is 0 then try to load the data, else load the data from the ArrayList . 如果是这种情况,则可以将信息保存在静态ArrayList并且可以在onCreate方法上检查其长度是否为0,如果为0,则尝试加载数据,否则从ArrayList加载数据。

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

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