Problem solved!
Let me explain. When you add
android:configChanges="orientation|keyboardHidden|screenSize"
you declare, that any changes will be done manually, so it's better to resign from this solution for another one, I'll describe below.
My problem Was keeping more complicated - then text - content from one layout to another. The key is, that orientation or screen size change causes activity destroy. After the destroy activity is created again, so method onCreate is called and the activity life cycle runs again.
It is a good practice, to put every data into a bundle if it's not restoreable from the database.
Let mi show you my code:
@Override
public void onSaveInstanceState(Bundle outState){
Log.d("debug", "onSaveInstanceState");
super.onSaveInstanceState(outState);
if(pictureAdded){
outState.putBoolean(ADDED, true);
outState.putString(PATH, currentPhotoPath);
}else{
outState.putBoolean(ADDED, false);
}
}
pictureAdded is my flag which is true if the user has already taken a picture, so the picture is stored in the memory, and it's file path exists.
This method is called before destroy. On create, we want to check if it is activity's first onCreate or not or at least if there was picture taken before.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form_layout);
pictureAdded = false;
if (savedInstanceState == null){
return;
}
if (savedInstanceState.containsKey(ADDED)){
if (savedInstanceState.getBoolean(ADDED)){
try{
currentPhotoPath = savedInstanceState.getString(PATH);
photoPreview.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
photoPreview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
setPic();
}
});
}catch (Exception e){
e.printStackTrace();
}
}
}
}
The first part of the onCreate() method is as usually. Then you have to check if bundle exists. Then it's a good practice to use the data stored at the key if and only if the key exists.
When we wish to add a picture to an ImageView, the thing is getting harder. Why? Because at this point, imageView's dimensions are 0px x 0px. How to solve it? The solution is posted above. Use treeobserver pattern. This part of code provides you, that the code in
@Override
public void onGlobalLayout(){
//here
}
will be called right after layout is created, so it's parts have dimensions. If you wish to do the code once, remove onGlobalLayoutListener.
Hope, to help someone.