in your manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zeta.workorder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.zeta.workorder.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.zeta.workorder.WorkListingActivity"
android:label="@string/title_activity_work_listing"
android:screenOrientation="landscape" >
</activity>
onCreate were called twice after implementing setRequestedOrientation
-
17-06-2023 - |
Question
I have a problem here where i need to display a data from a database and view it in my app in a form of table. Everything went well but since the table column is quite long(due to the data received from the database), i decided to set the orientation to landscape for easy view (i guess). After implementing this code,
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
The onCreate() function got called twice. I knew it got called twice since ive included the log.i. All of my log.i got called twice. My log should display this:
03-19 14:13:53.401: I/loop check(30510): loop
03-19 14:13:54.112: I/jObjLength(30510): 5
03-19 14:13:54.112: I/JSON(30510): jsonObj = work0
03-19 14:13:54.162: I/JSON(30510): jsonObj = work1
03-19 14:13:54.162: I/JSON(30510): jsonObj = work2
03-19 14:13:54.172: I/JSON(30510): jsonObj = work3
03-19 14:13:54.172: I/JSON(30510): jsonObj = work4
But instead, it shows this:
03-19 14:30:12.801: I/loop check(30510): loop
03-19 14:30:12.921: I/jObjLength(30510): 5
03-19 14:30:12.921: I/JSON(30510): jsonObj = work0
03-19 14:30:12.931: I/JSON(30510): jsonObj = work1
03-19 14:30:12.941: I/JSON(30510): jsonObj = work2
03-19 14:30:12.951: I/JSON(30510): jsonObj = work3
03-19 14:30:12.951: I/JSON(30510): jsonObj = work4
03-19 14:30:12.981: I/loop check(30510): loop
03-19 14:30:13.042: I/jObjLength(30510): 5
03-19 14:30:13.042: I/JSON(30510): jsonObj = work0
03-19 14:30:13.042: I/JSON(30510): jsonObj = work1
03-19 14:30:13.052: I/JSON(30510): jsonObj = work2
03-19 14:30:13.052: I/JSON(30510): jsonObj = work3
03-19 14:30:13.062: I/JSON(30510): jsonObj = work4
This happen after i implement the setRequestedOrientation. Can anybody explain why thiss is happening and what are the solution for this. Thanks
heres my onCreate class
public void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Log.i("loop check", "loop");
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_work_listing);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
tblLay = (TableLayout) findViewById(R.id.tblLayout);
jsonParser = new JSONParser();
jObj = jsonParser.getJSONFromUrl(URL);
txtOrderN = new TextView(getApplicationContext());
txtCabinetN = new TextView(getApplicationContext());
txtAssignP = new TextView(getApplicationContext());
txtActualP = new TextView(getApplicationContext());
txtStatS = new TextView(getApplicationContext());
String jsonObj;
for (int i = 0; i < jObj.length(); i++) {
jsonObj = "work";
jsonObj = jsonObj + i;
try {
tblRow = new TableRow(getApplicationContext());
Log.i("JSON", "jsonObj = " + jsonObj);
txtOrderN.setText(jObj.getJSONObject(jsonObj).getString(
"orderN"));
txtCabinetN.setText(jObj.getJSONObject(jsonObj).getString(
"cabinetN"));
txtAssignP.setText(jObj.getJSONObject(jsonObj).getString(
"assignP"));
txtActualP.setText(jObj.getJSONObject(jsonObj).getString(
"actualP"));
txtStatS.setText(jObj.getJSONObject(jsonObj).getString("statS"));
txtOrderN = new TextView(getApplicationContext());
txtCabinetN = new TextView(getApplicationContext());
txtAssignP = new TextView(getApplicationContext());
txtActualP = new TextView(getApplicationContext());
txtStatS = new TextView(getApplicationContext());
tblRow.addView(txtOrderN);
tblRow.addView(txtCabinetN);
tblRow.addView(txtAssignP);
tblRow.addView(txtActualP);
tblRow.addView(txtStatS);
tblLay.addView(tblRow);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error " + e,
Toast.LENGTH_LONG).show();
Log.i("Error ", "" + e);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zeta.workorder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.zeta.workorder.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.zeta.workorder.WorkListingActivity"
android:label="@string/title_activity_work_listing" >
</activity>
</application>
</manifest>
La solution 2
Autres conseils
Just add property
android:configChanges="keyboardHidden|orientation|screenSize"
in manifiest in <activity>
tag
Source : Android onCreate() Method called twice when device rotated. (ICS)