I'm working with an adapter(viewpager) and when the app changes orientation it loses its information. Which is the easiest way to conserve this information? I was going to use savedInstanceState inside the adapter but it doesn't recognise it.
Asked
Active
Viewed 840 times
0
-
Could you please show us what you have so far, what you've tried, and what your saving? – Nate-Wilkins Aug 13 '12 at 16:22
-
I have some layouts where there're edittext managed by Viewpager. If I change the orientation of the phone it loses information. I know I have to save them before but I can't use savedInstanceState inside the adapter. I have thougth in: create a plain text, create a little database, refresh every x seconds from the class that creates the adapter and retrieve info from variables but I think those things are so complex for so silly operation – Learning from masters Aug 13 '12 at 16:28
-
It's not about I have an error in this line. It's about how can I do this in an easy way? The steps, the line to follow for code it. – Learning from masters Aug 13 '12 at 16:59
1 Answers
0
Finally, I have done it following the advice of Jofre as he pointed in Activity restart on rotation Android and looking in http://developer.android.com/intl/es/guide/topics/resources/runtime-changes.html
YOU MUST NOT MODIFY MANIFEST. DON'T ADD android:configChanges
SOLUTION:
//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
}
}

Community
- 1
- 1

Learning from masters
- 2,032
- 3
- 29
- 42