0

我正在使用适配器(viewpager),当应用程序更改方向时,它会丢失其信息。保存这些信息的最简单方法是什么?我打算在适配器中使用 savedInstanceState 但它无法识别它。

4

1 回答 1

0

最后,我按照 Jofre 的建议完成了它,正如他在Activity restart on rotation Android中指出的那样, 并查看了http://developer.android.com/intl/es/guide/topics/resources/runtime-changes.html

您不得修改清单。不要添加 android:configChanges

解决方案:

//ALL THE VARIABLES YOU NEED TO SAVE IN YOUR ADAPTER PUT THEM AS PUBLIC, IE: 
//public int a = 2;
//public String b = "a";

Public class Yourclass extends Activity{

Classofyouradapter adapter;

@Override   
public void onCreate(Bundle savedInstanceState) {       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourlayout);

    final Save_object data = (Save_object) getLastNonConfigurationInstance();

    adapter = new Classofyouradapter(your variables);
    if (data != null) {
            fillit(data);
    }
    myPager = (ViewPager) findViewById(R.id.yourpanelpager);    
    myPager.setAdapter(adapter);    
}

@Override
public Object onRetainNonConfigurationInstance() {
    final Save_object data = new Save_object();
    return data;        
}

public class Save_object{
    int a = adapter.a;   
    String b = adapter.b;
    //Rest of variables
}

public void fillit(Save_object data){
    adapter.a= data.a;   
    adapter.b= data.b;          
    //Rest of variables 
}

}
于 2012-08-13T22:37:36.103 回答