0

I've a LinearLayout with a default background and I change the background by click on a button. By orientation the screen the background is reset to default background. I don't know why.

private Drawable backgroundImage;

public void InitializeUI() {
    parentLayout = (LinearLayout) findViewById(R.id.fragment_parent);
    if(backgroundImage != null) {
        parentLayout.setBackground(backgroundImage);
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    InitializeUI();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.activity_main);
    InitializeUI();
}

Changing the background with

Drawable d = new BitmapDrawable(getResources(), selectedBitmap);
this.backgroundImage = d;
parentLayout.setBackground(d);
Azcaq
  • 43
  • 1
  • 8
  • You also may want to read what the [Android Docs](http://developer.android.com/guide/topics/resources/runtime-changes.html) have to say – codeMagic Mar 30 '15 at 20:04
  • I don't found a solution – Azcaq Mar 30 '15 at 20:06
  • I just gave you a link with 21 answers plus the documentation. If you don't have a solution from those then you need to explain how those aren't working for you. – codeMagic Mar 30 '15 at 20:07
  • The link are not the same problem. My problem is the background not a thread – Azcaq Mar 30 '15 at 20:09
  • Thanks the documentation has helped me – Azcaq Mar 30 '15 at 20:12
  • Yes, but it is for the same reason. You said you don't know why. Because the Activity is being recreated if you don't handle it. Read through those other answers, the links provided with them, and the docs. Then explain what you don't understand about it. You are calling `setContentView(R.layout.activity_main);` in `onConfigurationChanged()` so it is as if you have just started the Activity – codeMagic Mar 30 '15 at 20:13

0 Answers0