0

Last night, this code (which I found here) showed all kinds of Log output as I rotated my Android device, values from 0 to 360 and maybe some negatives.

But it doesn't work today. No Log output. I put a breakpoint on the Log statement; no stop when debugging.

I changed a few lines this morning to set a value based on value of orientation and then the problem (no output) surfaced, so I used Show history to revert back to the version of MainActivity.java that was last active last night. But no output and no stop at Log statement when debugging.

What explains this?

import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.OrientationEventListener;

public class MainActivity extends Activity
{
    OrientationEventListener myOrientationEventListener ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        myOrientationEventListener = new OrientationEventListener(this,
                SensorManager.SENSOR_DELAY_NORMAL)
        {
            @Override
            public void onOrientationChanged(int orientation) 
            {
                Log.w("Orient", orientation + "");
                myOrientationEventListener.enable();
            }
        };
    }
}

** EDIT **

As requested, here's AndroidManifest.xml, unchanged since 2/19, according to Show history, but I'll gladly add permission; but how did it work last night? [Also: Added line that stands out inside <activity as suggested in first Answer.]

        <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
                package="com.dslomer64.nowweregettinsomewheres.fragments"
                android:versionCode="1"
                android:versionName="1.0" >

                <uses-sdk
                        android:minSdkVersion="11"
                        android:targetSdkVersion="16" />

                <application
                        android:allowBackup="true"
                        android:icon="@drawable/ic_launcher"
                        android:label="@string/app_name"
                        android:theme="@style/AppTheme" >
                        <activity
                                   android:name="com.dslomer64.nowweregettinsomewheres.fragments.MainActivity"

android:configChanges="orientation|keyboardHidden"

                                android:label="@string/app_name" >
                                <intent-filter>
                                        <action android:name="android.intent.action.MAIN" />

                                        <category android:name="android.intent.category.LAUNCHER" />
                                </intent-filter>
                        </activity>
                </application>

        </manifest>

Here's Android Studio 1.5.1 output:

            02-24 12:55:12.554 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection

            02-24 12:55:12.632 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection

            02-24 12:55:12.632 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: getExtractedText on inactive InputConnection

            02-24 12:55:12.670 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection

            02-24 12:55:12.672 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection

            02-24 12:55:12.673 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: getSelectedText on inactive InputConnection

            02-24 12:55:12.674 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection

            02-24 12:55:12.674 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection

            02-24 12:55:13.031 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection

            02-24 12:55:13.032 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection

            02-24 13:00:07.936 14836-14836/com.bffmedia.hour8app.intro W/InputMethodManager: startInputInner : InputBindResult == null

            02-24 13:00:07.946 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection

            02-24 13:04:06.824 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection

            02-24 13:04:06.973 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: getExtractedText on inactive InputConnection

            02-24 13:04:07.022 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection

            02-24 13:04:07.022 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection

            02-24 13:04:07.023 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: getSelectedText on inactive InputConnection

            02-24 13:04:07.025 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection

            02-24 13:04:07.025 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection

            02-24 13:04:07.402 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection

            02-24 13:04:07.402 14836-14836/com.bffmedia.hour8app.intro W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
Community
  • 1
  • 1
DSlomer64
  • 4,234
  • 4
  • 53
  • 88
  • Show your manifest. The activity needs to have permission to handle orientation changes – Gavriel Feb 24 '16 at 18:13
  • 1
    Actually I had a look at the other SO question. Maybe if you would try one of the upvoted answers, not the one that got 0 votes, it will work :) – Gavriel Feb 24 '16 at 18:23
  • How did it work last night??? I'm baffled. But I'll try one of the others. – DSlomer64 Feb 24 '16 at 18:23
  • Well I don't know what could changed since yesterday (your code? you tried on a different device? your manifest? your API level?, ... ) or maybe just the way you rotate the phone (fast/slow)? However I'm not even sure you need this sensor thing, every Activity is restarted when orientation changed, you should be able to detect it in onCreate() – Gavriel Feb 24 '16 at 18:27
  • @Gavriel--I took your advice and found a more reliable solution in my Answer below. – DSlomer64 Mar 12 '16 at 20:28

2 Answers2

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

You need configChanges="orientation" in your manifest.

Gavriel
  • 18,880
  • 12
  • 68
  • 105
0

Apparently, Show history doesn't necessarily include last change to code. Apparently, before rushing off to an appointment last night, I had moved the .enable() statement outside the on block. The code now works perfectly, as it did last night without change to AndroidManifest.xml.

        myOrientationEventListener = new OrientationEventListener(this,
                SensorManager.SENSOR_DELAY_NORMAL)
        {
            @Override
            public void onOrientationChanged(int orientation) 
            {
                ///////// NOT HERE myOrientationEventListener.enable(); //////////
                Log.w("Orient", orientation + "");
            }
        };
        Log.w("Listener", " can detect orientation: " + myOrientationEventListener.canDetectOrientation() + " " );

        myOrientationEventListener.enable(); // HERE ////////////////////////

I requested an edit of the Answer that the code came from to spare the next guy who uses it. That Answer is fine as long as the enable is placed logically.

* HOWEVER (EDIT) *

That listener doesn't quite work right anyway in my onCreate method. As suggested by @Gavriel, I found a better method:

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) ... do whatever
DSlomer64
  • 4,234
  • 4
  • 53
  • 88