Reputation: 6697
I am adding TextViews to my layout and I want to keep them when screen orientation changes.
I tried the following code, but it's not working.
public class DetailsActivity extends Activity {
private HashMap<View, TextView> views = new HashMap<View, TextView>();;
@Override
public Object onRetainNonConfigurationInstance() {
return views;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.layout44);
findViewById(R.id.layout44_11).setOnLongClickListener(onLongClickListener);
...
HashMap<View, TextView> data = (HashMap<View, TextView>)this.getLastNonConfigurationInstance();
if (data == null) {
System.out.println("is null");
} else {
for(View key : data.keySet()) {
ViewGroup parent = (ViewGroup) key;
System.out.println(key + " " + data.get(key));
System.out.println(parent.getChildCount());
System.out.println("child: " + parent.getChildAt(0));;
parent.removeView(data.get(key));
data.get(key).setText("asd");
data.get(key).setVisibility(View.VISIBLE);
parent.addView(data.get(key));
}
System.out.println("-------------------");
}
}
private View.OnLongClickListener onLongClickListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ViewGroup parent = (ViewGroup) v;
if(parent != null) {
TextView tv = new TextView(getApplicationContext());
tv.setText("TextView");
views.put(parent, tv);
parent.addView(tv);
}
return false;
}
};
}
I'm getting exception:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Why? What am I doing wrong? How to solve it?
Upvotes: 0
Views: 2388
Reputation: 25864
When you change screen orientation, what you are in effect doing is destroying your Activity and then starting it again freshly.
If you have added 10 TextViews in your onCreate() method, don't worry, they will be added exactly as they were previously without any interaction.
However, if your Activity has had some user input or some processing which has made the Activity stateful (The Activity has had button presses or data submitted) you will need to use onSavedInstanceState(Bundle out)
to save all of the state information (Basic type, NOT Views) and then in onCreate()
you can pull this information out of the Bundle to recreate the state.
never pass Views
around Activities (or between Orientation changes). Views are owned by their Activities, when they are passed around they bring their original Activities with them.
If you are new to Android think of it like this: If you are in an Art Gallery and the building is about to be torn down and a new Art Gallery is going to be built (You switch from Landscape to Portrait) then you'll want to take all of the art from the First Gallery and then put it in the new Gallery. You won't rip the Walls (Views) out of the First Gallery to build the second Gallery, you'll build the second Gallery and then hang the art onto the walls (Views) of the new Gallery.
Upvotes: 3