简体   繁体   中英

Listener for Orientation: Portrait and Landscape?

I've an app which calculate the subnettable.
But if I change the orientation the TextViews are empty.

I search for an listener for this. Alternatives are welcome to :D In the listener I'd recalc the table.

Use the following code in you activity:

@Override
public void onConfigurationChanged(Configuration myConfig) {
    super.onConfigurationChanged(myConfig);
    int orient = getResources().getConfiguration().orientation; 
    switch(orient) {
        case Configuration.ORIENTATION_LANDSCAPE:
            // handle landscape here
            break;
        case Configuration.ORIENTATION_PORTRAIT:
            // handle portrait here
            break;
        default:
        }
}

In your manifest > activity tag add:

    <activity
        android:name="com.activity.name"
        android:configChanges="orientation|screenSize"

This will prevent your activity from getting recreated when you rotate or change your orientation and hence you wont get blank TextView (will retain your previous value)

Use OrientationEventListener:

orientationListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {
    public void onOrientationChanged(int orientation)
    {
        //do something
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM