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,
Thank you, this really help me.
ReplyDeleteGood luck
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.
ReplyDeleteData 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
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.
ReplyDeletejava training in omr | oracle training in chennai
java training in annanagar | java training in chennai
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.
ReplyDeletepython online training
python training in OMR
python training in tambaram
I recently came across your blog and have been reading along. I thought I would leave my first comment.
ReplyDeleteBlueprism online training
Blue Prism Training in Pune
Well researched article and I appreciate this. The blog is subscribed and will see new topics soon.
ReplyDeleteangularjs Training in bangalore
angularjs Training in btm
angularjs Training in electronic-city
angularjs online Training
angularjs Training in marathahalli
ReplyDeleteThanks for the great blog! keep posting.
Selenium Training in Chennai
Selenium Training
iOS Training in Chennai
French Classes in Chennai
Big Data Training in Chennai
JAVA Training in Chennai
JAVA Course in Chennai
Java training institute in chennai
Useful blog with lots of information. Keep sharing.
ReplyDeleteUiPath Training in Chennai
UiPath Training
RPA Training in Chennai
Blue Prism Training in Chennai
AWS Training in Chennai
Angularjs Training in Chennai
Data Science Course in Chennai
Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing.
ReplyDeleteAWS training in chennai | AWS training in anna nagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery
Beautiful Blog. First the Site Presentation is nice Contents are very Clear. thanks for Updating this Blog.
ReplyDeleteData Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery
It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.good ob.
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
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,
ReplyDeleteI’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
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.
ReplyDeletedata scientist training in hyderabad
perde modelleri
ReplyDeletesms onay
TURKCELL MOBİL ÖDEME BOZDURMA
Nftnasilalinir.com
ankara evden eve nakliyat
trafik sigortası
dedektör
Kurma Website
Ask romanlari
Smm Panel
ReplyDeleteSmm Panel
iş ilanları
instagram takipçi satın al
Hırdavat
Beyazesyateknikservisi.com.tr
servis
Tiktok Para Hilesi İndir