3

I would like to accomplish the following:

  1. When I turn my phone into landscape mode, I want an activity to be started.
  2. When I turn it back again into portrait mode, I want that activity to be closed again.

In essence I'd like to know when my orientation changed, but without changing the layout. Is that possible?

Docteur
  • 1,235
  • 18
  • 34
deimos1988
  • 5,896
  • 7
  • 41
  • 57
  • I will be watching this one. I have an issue with a widget that changes with no update because of an orientation change. I too would like to know how to detect it. – durbnpoisn Apr 07 '14 at 14:51
  • Do you want to call new Activity altogether or current Activity be restarted in Landscape mode? – CRUSADER Apr 07 '14 at 14:56
  • @CRUSADER I would like to call a new activity. – deimos1988 Apr 07 '14 at 14:57
  • I think you might be better off using a different layout for landscape. It could be identical to your portrait layout but have an additional fragment in it. – Karakuri Apr 07 '14 at 15:04
  • Use a SensorManager to listen for raw events: http://stackoverflow.com/a/9022405/833647 – Ken Wolf Apr 07 '14 at 15:23

2 Answers2

0

I am not 100% sure what you are asking, because 1 and 2 do not equate to your final question. Either way, the answer you are looking for is onConfigurationChanged.

If you already have an activity running you can tell Android in the AndroidManfest that you want to handle rotation. Then override onConfigurationChange to launch a new activity. In that activity also tell android you want to handle configuration changes and in the ondonfigurationchange it can call finish() to go back to the previous activity.

See: Android Activity onConfigurationCjhanged

Put this code in your android manifest for the activities:

<activity android:name=".MyActivity" android:label="@string/appName"
                android:configChanges="orientation|keyboard|keyboardHidden">
Kaediil
  • 5,465
  • 2
  • 21
  • 20
0

Android Developer Guide here

the following manifest code declares an activity that handles both the screen orientation change and keyboard availability change:

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

Now, when one of these configurations change, your Activity does not restart. Instead, your Activity receives a call to onConfigurationChanged().

The following onConfigurationChanged() implementation checks the current device orientation:

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

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        //call new Activity here  
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}
CRUSADER
  • 5,486
  • 3
  • 28
  • 64
  • Thank you for your reply, but won't it still change the layout since I didn't declare orientation="portrait"? – deimos1988 Apr 07 '14 at 15:02
  • You lost me on this one... you dont want your layout to change to landscape?? – CRUSADER Apr 07 '14 at 15:05
  • No, I only want to check the orientation and start a new activity, nothing more :) – deimos1988 Apr 07 '14 at 15:07
  • Just try out the above mentioned code... and update us on further problems you are facing... Since you are calling new Activity.. the previous activity in which you added this code will be added in stack.. – CRUSADER Apr 07 '14 at 15:10
  • OK, but there is another problem: When I touch the second activity, it should also close; so if I'm in landscape-mode, the layout of the first activity will be changed, I suppose? – deimos1988 Apr 07 '14 at 15:14
  • @deimos1988 your questions get more unclear now.. You can update your question adding screenshots of what you are currently observing and what you actually desire... those screenshots will really help.. You can also [refer here for better understanding of handling screen layout](http://www.codeproject.com/Articles/422431/Handling-screen-layout-changes-in-Android) – CRUSADER Apr 07 '14 at 15:19