3

I have the following problem: I have an image that I want to disappear when I change my phone from portrait to landscape. It works perfectly fine when I am on the current activity and I move orientations. It also works completely fine if I go to another activity and come back. But the problem is that after I return to my main page, onConfigurationChanged() will not be called the first time I change orientation. The image will show/not show correctly when I return, but when I first change the orientation after returning onConfigurationChanged() will not be called and the image will either stay/not stay until the second time I change the orientation. Any help you guys have would be extremely appreciated! Here's a couple parts of my code. Ive put the configChange- orientation permission in the manifest already so thats not a problem.

    @Override
    public void onConfigurationChanged(Configuration newConfig) {L.p("configList",5200184);
        super.onConfigurationChanged(newConfig);

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE&&isVertical==true) {

            Logo.setVisibility(ImageView.GONE);
            mainLogo.setVisibility(ImageView.GONE);
            isHorizontal=true;

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT&&isHorizontal==true){

            Logo.setVisibility(ImageView.VISIBLE);
            mainLogo.setVisibility(ImageView.VISIBLE);
            isVertical=true;
        }
      }




    @Override
    protected void onResume() {L.p("onResumeList", 5200113);
        super.onResume();
        Context ctx = getApplicationContext();
  WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
  Display display = wm.getDefaultDisplay();

        if((display.getOrientation() == Surface.ROTATION_90) || (display.getOrientation() == Surface.ROTATION_270))
            {//Horizontal
            L.p("you are resuming horizontal",5200124);
            Logo.setVisibility(ImageView.INVISIBLE);
    mainLogo.setVisibility(ImageView.INVISIBLE);    
    isHorizontal=true;
    //isVertical=false;
            }
        else {//Vertical
            L.p("you are resuming vertical",5200131);
    Logo.setVisibility(ImageView.VISIBLE);
    mainLogo.setVisibility(ImageView.VISIBLE);
    isVertical=true;
  //    isHorizontal=false;
    }
        tracker.trackPageView("/deviceList");
    }

Thanks!

Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75

4 Answers4

0

There is probably a flag somewhere causing the problem. And, we will probably never know what the problem is without seeing the full code, unfortunately.

But, please do not use configChange hacks. (https://plus.google.com/109306462100800920982/posts/LuYGURGdX7Q for a good explanation of the extremely rare case of when it is okay.)

Most likely, the onConfigurationChanged method does not need to be overridden at all here. Make a different layout for when the Activity is recreated on the configuration change.

Eric Cochran
  • 8,414
  • 5
  • 50
  • 91
0

No need to override orientation change. Just declare a new layout file in the layout-land folder that doesn't include the image (or in which the image is set to invisible/gone). When changing orientation, your activity will load that layout in onCreate.

josephus
  • 8,284
  • 1
  • 37
  • 57
  • I've heard thats bad practice, but thanks for the tip. I just want to know why onConfigurationChanged() isn't called the very first time I change orientations after returning from another activity. If i move it from horizontal to vertical, the image is still gone. But then i move back to horizontal and then vertical and it appears as it is supposed to. – Tommy Devoy Feb 15 '12 at 01:11
  • 1
    I personally never heard of layout-* folders being "bad practice". Anything as to why it's considered as such? – josephus Feb 15 '12 at 01:35
  • @tdevoy It's far better practice to make separate layouts over hacking with configChange. – Eric Cochran Aug 12 '14 at 22:42
0

Why do you check isHorizontal and isVertical? Is it possible that one of those is false and that is causing it to fail the if conditional?

ByteMe
  • 1,436
  • 12
  • 15
  • 1
    No those definitely work right. I only put them in as an extra guard to double check after onResume that it knows the previous orientation correctly. Everything works right, its just that the very first time I return to the activity and change the orientation of the phone, the orientation change is not recognized and i have to change orientation twice before onConfigurationChanged is called. Thank you though, any other ideas? – Tommy Devoy Feb 15 '12 at 01:06
0

The onConfigurationChanged() method will not be called unless you declared the respective attributes under the <activity> in AndroidManifest.xml:

android:configChanges="orientation"
Victor Wong
  • 2,486
  • 23
  • 32
  • Yeah thats not the problem. The problem is that the when I return from another activity, onConfigurationChanged() will not be called the very first time I change orientations, but it will be called every time after that. Do you have any idea on that? – Tommy Devoy Feb 15 '12 at 01:12
  • Not quite sure the reason, but did you manually set the orientation of the Activity? If so, the method will also not be called correctly. I suggest you to create a new Android project and not to put any codes instead of onConfigurationChanged() and startActivity(), so that you could observe the behaviour. – Victor Wong Feb 15 '12 at 01:32
  • Never, ever use configChange just because you can. https://plus.google.com/109306462100800920982/posts/LuYGURGdX7Q Check Adam's great comment there. I will not rest until Android devs stop doing this. @VictorWong – Eric Cochran Aug 12 '14 at 22:41