Sunday, June 12, 2011

Handling Screen Orientation - Part 2


How to handle rotation?
Now if you choose to rotate your activity with display either specifying “rotate as sensor” or using “orientation setting” you must handle the steps that are taken when the display is rotated.
Whenever the window rotates, your activity is destroyed and recreated, and “onCreate(Bundle)” is executed again.
You may need to save the activity state before this happens. To achieve that you can override the
public void onSaveInstanceState(Bundle outState)
For Example: You may want to save the Value of a String array before screen orientation change restarts your activity.
To do this override the onSaveInstanceState method of Activity class like this:
public class SampleActivity extends Activity{
      String[] testArray = null;
      public static final String TEST_ARRAY = “KEY_FOR_ARRAY”;

      @Override
      public void onCreate(Bundle State){
            if(State != null){
testArray =
State.getStringArray(SampleActivity.TEST_ARRAY);
            }
            else{
                  testArray = new String[6];
}
      }

      @Override
public void onSaveInstanceState(Bundle outstate){
outstate.putStringArray(SampleActivity.TEST_ARRAY,
      testArray);
}
}

Otherwise your activity will use the default implementation of this function which saves some amount of state.
Some of the values that you can save before orientation change are as follows:
  • Serializable and its subclasses.
  • String
  • Parcelable and its Subclasses
  • boolean, int, float.
  • Arrays etc.

However you may not want to store the values because this may slow down your application you can attain faster screen orientation by caching the data before orientation changes.
Your activity can cache data before rotation this can be accomplished by overriding
public Object onRetainNonConfigurationInstance()
Let’s take the above example again but instead of saving the String array we’ll cache the data and retrieve reference to that after restart.
public class SampleActivity extends Activity{
      String[] testArray = null;
      public static final String TEST_ARRAY = “KEY_FOR_ARRAY”;

      @Override
      public void onCreate(Bundle State){
testArray = (String[])getLastNonConfigurarionInstance();
if(testArray == null)
      testArray = new String[6];
      }

      @Override
public Object onRetainNonConfigurationInstance(){
      return testArray;
}
}
Change orientation at runtime
You can use Activity.setRequestedOrientation() from your activity to set that orientation
e.g. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
if you do not use this the default value is SCREEN_ORIENTATION_UNSPECIFIED and you must not specify any orientation in Applications manifest XML. And each time orientation is changed the activity is stopped and restarted.
Check orientation settings
You can query the orientation settings for device using
public static int myOrient;
myOrient = this.getResources().getConfiguration().orientation;

this function will return either of the following values:
ORIENTATION_LANDSCAPE or ORIENTATION_PORTRAIT, it can also return ORIENTATION_SQUARE on a square device.

<<Handling Screen Orientation - Part 1

Cheers!!
Vivek Jain
Hello Everyone, I came across an application which allowed me to produce nice effects on Images that are in my phone and Images that I take from my phones camera, its available here, 
 

16 comments:

  1. Thank you, this really help me.
    Good luck

    ReplyDelete
  2. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Data Science Training in Chennai
    Data science training in bangalore
    Data science online training
    Data science training in pune
    Data Science training in kalyan nagar
    Data Science training in OMR
    selenium training in chennai

    ReplyDelete
  3. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

    ReplyDelete
  4. After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
    python online training
    python training in OMR
    python training in tambaram

    ReplyDelete
  5. I recently came across your blog and have been reading along. I thought I would leave my first comment.
    Blueprism online training

    Blue Prism Training in Pune

    ReplyDelete
  6. Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing.
    AWS training in chennai | AWS training in anna nagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

    ReplyDelete
  7. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say,
    I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers?
    I’d appreciate it.
    Java training in Chennai

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Online Training


    ReplyDelete
  8. I am truly getting a charge out of perusing your elegantly composed articles. It would seem that you burn through a ton of energy and time on your blog. I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome.
    data scientist training in hyderabad


    ReplyDelete
  9. Very nice job... Thanks for sharing this amazing and educative blog post! data science training in surat

    ReplyDelete