2

I'm using MonoDroid but an equivalent Java answer can still help.

I'm using a portrait layout and a landscape layout so, if possible, I want to use the Android screen orientation to automatically destory/create activities.

My app is using TextToSpeech so in the activity's OnPause() I am stopping it, which works well when the Home key is pressed or an incoming call is happening. However I don't want to stop the TextToSpeech on a screen orientation change from the user.

Is there a simple way of detecting this change so that TextToSpeech isn't interrupted?

My activity's OnStop() code:

protected override void OnPause()
{
    // I need this for Home key, intercepting phone calls, etc.
    // But how can I prevent this for a screen orientation change?
    // Need to enable screen orientation to get my portrait/landscape views
    if(Text2Speech.IsTextToSpeechInitialised && Text2Speech.TextToSpeech != null)
        Text2Speech.TextToSpeech.Stop();

    base.OnPause();
}
Kijewski
  • 25,517
  • 12
  • 101
  • 143
Mark Eaton
  • 25
  • 1
  • 3
  • 3
    Have you looked at using `android:configChanges="orientation"` for your `Activity` in the manifest? This specifies that you want to handle orientation changes yourself. Override `onConfigurationChanged` in your `Activity` and do anything you might need to do (change layout etc). – Squonk Jan 08 '12 at 08:51
  • I saw that and it may be the approach I have to go. If possible I would like Android to take care of it all for me when destroying my portrait activity / creating my landscape activity, and vice-versa. I was just wondering if there was an easy way to detect an orientation change during an OnPause() – Mark Eaton Jan 08 '12 at 09:06
  • 1
    No there's no easy way of knowing. The `Activity` lifecycle is subject to various things (incoming phone call for example). During the 'out' phase of an `Activity` (pause, stop destroy) there isn't a system mechanism to explain 'why' this is happening. The best you've got is with handling the change yourself. The `Configuration` object passed into `onConfigurationChanged(...)` will at least help you identify this is an orientation change at which point you'll just have to use `setContentView(...)` with the correct layout. It's fiddly but not too bad. – Squonk Jan 08 '12 at 09:53
  • I heard that fragments might help. According to here: http://stackoverflow.com/questions/8474104/android-fragment-lifecycle-over-orientation-changes and to SDK docs, Fragments will stay active & don't get destroyed over config changes like orientation. – Sebastian Roth Jan 08 '12 at 12:02

1 Answers1

3

As others have said in the comments on the question, I think you'll want to handle configuration changes yourself here. There are other configuration changes that can cause your activity to be restarted as well, such as revealing the device's hardware keyboard.

In Mono for Android you can specify which of these you want to handle yourself in the ActivityAttribute:

[Activity(ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden)]
public class MainActivity : Activity
Greg Shackles
  • 10,009
  • 2
  • 29
  • 35
  • 1
    Thanks all the answers everyone. It confirmed there was no simple way of doing this, but handling the orientation changes myself would be for the best – Mark Eaton Jan 09 '12 at 08:53
  • Just so you know, I converted all of the Java ways of handling this issue. Android recreates the Activity on Rotation, so any reference to the previous context will cause a crash. I tried ALL of the Java solutions and none of them worked very well in Mono for Android, mainly because there are two GCs. The best way to do this, at least in Mono for Android is this way, as long as you are very diligent in cleaning up your resources in your OnDestroy you should be 100% ok! – emalamisura Jan 19 '12 at 20:40
  • @Greg, Thank you for your answer. Do you know what the ConfigurationChanges equivalent of screenSize that is needed for API 13 and above? [link](http://stackoverflow.com/questions/5620033/onconfigurationchanged-not-getting-called) as referenced in the second question? – TChadwick Oct 11 '12 at 22:02
  • Thanks to your link, I saw that the Enum for it is ScreenSize, however, it doesn't appear to be available in my version of MonoDroid, (I'm completely up to date with the latest SDK). Any idea why that might be? – TChadwick Oct 12 '12 at 14:19
  • ScreenSize was added in API level 13, so you need to target at least that level to use it: http://developer.android.com/guide/topics/manifest/activity-element.html#config – Greg Shackles Oct 12 '12 at 15:53