Android: screen rotation, on destroy and services connundrum
Asked Answered
E

3

2

I've modified the bluetooth chat example from the SDK demos to be able to control an arduino powered bluetooth LED matrix. Using the chat program, I can send messages to the display via bluetooth. I have a problem though. I've done two screen layouts, a portrait and a landscape. This way I can have the interface occupy the most space on the phone, regardless of orientation.

The problem is that if the phone is rotated, OnDestroy() is called, to reload the new layout (landscape, or portrait). In the OnDestroy() routine I also destroy the bluetooth link, if it is established:

   public void onDestroy() {
        super.onDestroy();
        // Stop the Bluetooth chat services
        if (mChatService != null)
            mChatService.stop();
        if (D)
            Log.e(TAG, "--- ON DESTROY ---");
    }

Reading other posts on here, I've found that you can prevent the service from being stopped by adding "android:configChanges="orientation"" to the Manifest. Doing this, when I rotate the screen, my bluetooth link to the display is no longer terminated, however now the screen doesn't redraw in landscape mode.

To fix this, I am thinking of removing the "if mchatservice..." section, which is terminating the connection, but then I will still need the code to run when the application is ultimately exited.

Is there a way to have the screen redraw when rotated, without terminating the connect? If not, I think I can always move the service code to the OnPause() event, however this will terminate the connection if the app ever looses forground focus.

Are there any other ways?

Thanks.

Eastsoutheast answered 8/2, 2012 at 1:48 Comment(0)
F
9

If you add "android:configChanges="orientation"" into your Manifest to prevent the activity from being destroyed and re-created, you might want to implement the method:

public void onConfigurationChanged(Configuration newConfig)

This method is executed every time the system configuration is changed, i.e. when you rotate the phone and orientation is changed. Inside this method you can re-apply a new layout for your activity:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Log.e(TAG, "ORIENTATION_LANDSCAPE");
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        Log.e(TAG, "ORIENTATION_PORTRAIT");
    }
}
Flabby answered 8/2, 2012 at 2:7 Comment(1)
Awesome. I just found something similar after searching for an hour on google, but your answer is more detailed (helping with the logging) I ended up adding this as well: setContentView(R.layout.main); Thanks!Eastsoutheast
I
0

to add to @alex.veprix to the point (and correct I must mention) answer, if you're building for devices with API level 13 or higher, don't forget to add ScreenSize attribute in the following way: android:configChanges="orientation|screenSize"

Details here: http://developer.android.com/guide/topics/manifest/activity-element.html

Inaccuracy answered 14/9, 2015 at 17:45 Comment(0)
T
0

The recommended way to do this is to have a TaskFragment handling your logic, and then some callbacks to your activity to sync with your UI.

See the following site for more detailed explanation: http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

Theologize answered 14/9, 2015 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.