tags:

views:

35

answers:

1

Hi,

Can you please tell me how can I keep the MediaRecorder keeps recording after an orientation change? I try looking into source code of packages/apps/SoundRecorder/src/com/android/soundrecorder/Recorder.java, I don't see it handles that cases.

Thank you.

+1  A: 

This might be because by default the system destroys and recreates the Activity when the orientation changes. You can tell the system that your app handles changes like these by modifying the activity's tag in the manifast like this:

<activity android:name=".UIActivity"
                  android:configChanges="orientation|keyboardHidden"
                  android:label="@string/app_name"> ...
</activity>

This way you can react to these changes overriding this method:

 @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
        super.onConfigurationChanged(newConfig); 
        //---code to redraw your activity here---
        //...
    }

Here, you can redraw your views to support landscape mode, or do simply nothing.

Scythe
I have a field/instance variable for "MediaRecorder mRecorder". How/what should I do to re-instantiate that after an orientation change so that the recording is continue? As you said the Activity is re-created when the orientation changes. Thank you.
michael
Do you want to support orientation change (landscape mode)? If so, then you only have to use the Activity's "setContentView(...)" method to set the layout to the landscape mode UI layout in the onConfigurationChanged() listener I showed above. If not, then you simply do nothing, you don't have to reinitialize the mRecorder variable, because it won't get destroyed.
Scythe
What if I have an activity which has tabs. Do I need to add 'android:configChanges="orientation|keyboardHidden"' for the Tab activity or each activity in the tab? Thank you.
michael
Honestly, I never tried this with a tabbed activity. I think you should just try it out. You can even test it in the emulator, 'Ctrl+F12' rotates it.
Scythe
I did what you suggested. But as I rotate the device, I see onConfigurationChanged() get called first AND THEN it calls onCreate() of my activity. So essentially it calls my setContentView(R.layout.something); twice.
michael