configChanges issue when rotating device between two activities [+VIDEO]

57 views Asked by At

I have two activity. The app starts, I start the second activity. I rotate the device. I go back to the first activity. Black area shows up during the animation. This is way I set

android:configChanges="keyboardHidden|orientation|screenSize"

in my activities. If I don't, everything works fine.

How do I fix this?

I know I have to implement onConfigurationChanged

Here's the code:

public class MainActivity extends Activity {
    @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         initUI();
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        initUI();
    }

    private void initUI()
    {
        setContentView(R.layout.activity_main);

        final Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            Intent subActivity = new Intent(MainActivity.this,SecondActivity.class);
                startActivity(subActivity);
            }
        });
    }
}

and second:

public class SecondActivity extends Activity {

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

}

Here's a video of that happens: http://www.youtube.com/watch?v=8NLNkE1eOK8

This is not, of course, the project that I'm working on but it's an example specifically created to let understand this issue.

Thanks.

1

There are 1 answers

0
Martin On BEST ANSWER

You're catching configuration changes, now you have to do something with it.

Within initUI() you should determine your new aspect ratio and location. A simple If-Else that sets sizes and LayoutParams based on which way the user rotated the screen. Instead, you're simply calling Activity2 with the same layout of activity_main. You could easily setContentView to activity_main_port or programmatically set sizes, layouts, etc.