How to change screen orientation, without creating a new activity on android? -
i not know if title correct, here happens.
i have application works differently on phone , on tablet, on phone shows portrait on tablet shows landscape.
to achieve created class called coreactivity extended activities , following:
public class coreactivity extends activity { protected boolean _landscape = false; public boolean isphone() { int layoutsize = getscreenlayoutsize(); return (layoutsize == configuration.screenlayout_size_small || layoutsize == configuration.screenlayout_size_normal); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (isphone() && !_landscape) { this.setrequestedorientation(activityinfo.screen_orientation_portrait); } else { this.setrequestedorientation(activityinfo.screen_orientation_landscape); } } protected int getscreenlayoutsize() { return (getresources().getconfiguration().screenlayout & configuration.screenlayout_size_mask); } }
my problem occurs when wish show screen on phone setup on landscape mode, use following:
@override protected void oncreate(bundle savedinstancestate) { _landscape = true super.oncreate(savedinstancestate); }
the problem is, on phone, if user holding phone on portrait mode (as 1 would, since of application in portrait mode) activity gets created , destroyed , recreated. if holding on landscape mode, created once.
my problem occurs because on oncreate method launch threads load data, , show fragments.
is there way avoid problem? there way launch activity start on portrait mode , not change it, or have not create twice?
thanks in advance can provide
recreating occurs because forcing to, calling setrequestedorientation
in order set screen orientation. don't need check , change code. can via xml file. can set different xml files depending on screen size. little bit hack there no guarantee in future.
on manifest file can force by:
<activity android:name="com.my.example.myactivity" android:screenorientation="landscape"/> // or portrait
so far understand want force portrait small sizes , landscape(or sensor) larger screen sizes. above configuration applies screen sizes. here tricky part: landscape
portrait
sensor
etc. integers defined here. might guess can write android:screenorientation=0
instead of landscape
. know beginning lessons of android, can define integers in xml files values might vary on screen size. so..
you should first create different integers.xml
files different screen sizes. i.e.:
values - integers.xml values-large - integers.xml
for values/integers.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <integer name="orientation">1</integer> // 1 portrait </resources>
for values-large/integers.xml
:
<?xml version="1.0" encoding="utf-8"?> <resources> <integer name="orientation">0</integer> // 0 landscape </resources>
and you have manipulate manifest file by:
<activity android:name="com.my.example.myactivity" android:screenorientation="@integer/orientation"/> // read constant xml files
you can use sensor
, fullsensor
, nosensor
, locked
, behind
, reverselandscape
, reverseportait
options too. warning you, hack solution.
Comments
Post a Comment