I am using fragments and when i change the orientation the application crash. Here is the log cat:
05-29 05:56:52.158: E/AndroidRuntime(1428): java.lang.Runt
There is another case I found If you use different XML for Portrait and Landscape. Then different root element or view may lead to crash for example --
My Portrait XML is --
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_page_back_gradient"
android:orientation="vertical"
tools:context="com.thepsi.beintent.LoginPage"
android:id="@+id/parent_login_page">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/linear_right_20dp"
android:layout_marginRight="@dimen/linear_right_20dp">
<ImageView
android:id="@+id/img_logo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="@drawable/beintent_logo" />
<EditText
android:id="@+id/editTextLoginEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/img_logo"
android:layout_marginTop="@dimen/margin_between_views_login_page"
android:background="@drawable/box_edittext"
android:hint="@string/email_login"
android:inputType="textEmailAddress"
android:padding="10dp"
android:textStyle="bold" />
<EditText
android:id="@+id/editTextLoginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editTextLoginEmail"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:background="@drawable/box_edittext"
android:hint="@string/password_login"
android:inputType="textPassword"
android:padding="10dp"
android:textStyle="bold" />
<CheckBox
android:id="@+id/checkBoxLoginPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editTextLoginPassword"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textColor="@android:color/white"
android:textStyle="bold"
app:buttonTint="@android:color/white" />
<TextView
android:id="@+id/txt_check_box_lbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/checkBoxLoginPage"
android:layout_alignTop="@+id/checkBoxLoginPage"
android:layout_gravity="center_vertical"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/checkBoxLoginPage"
android:text="@string/remember_me_login"
android:textColor="@android:color/white"
android:textSize="@dimen/forgot_rember_text_size"
android:textStyle="bold" />
<TextView
android:id="@+id/txt_question_mark"
android:layout_width="24dp"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBoxLoginPage"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:background="@drawable/circle_background_red"
android:gravity="center"
android:text="@string/questionMark"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txt_question_mark"
android:layout_alignLeft="@+id/txt_check_box_lbl"
android:layout_alignTop="@+id/txt_question_mark"
android:layout_gravity="center_vertical"
android:layout_toRightOf="@id/txt_question_mark"
android:text="@string/forgot_password_login_page"
android:textColor="@android:color/white"
android:textSize="@dimen/forgot_rember_text_size"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_question_mark"
android:layout_marginTop="@dimen/margin_between_views_login_page"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btn_login"
android:layout_width="0dp"
android:layout_height="@dimen/button_height_loginpage"
android:layout_gravity="center"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@drawable/common_btn_selecter"
android:ems="10"
android:onClick="LoginButtonClicked"
android:text="@string/log_in_login_page"
android:textColor="#fff"
android:textSize="@dimen/login_page_button_text_size"
android:textStyle="bold" />
<Button
android:id="@+id/btn_sign_up"
android:layout_width="0dp"
android:layout_height="@dimen/button_height_loginpage"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:background="@drawable/common_btn_selecter"
android:ems="10"
android:onClick="SignUpButtonClicked"
android:text="@string/sign_up_login_page"
android:textColor="#fff"
android:textSize="@dimen/login_page_button_text_size"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:paddingBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="end"
android:onClick="OnContactUsClicked"
android:text="@string/contact_us"
android:textColor="@android:color/white"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_for_seperator"
android:layout_marginLeft="@dimen/margin_for_seperator"
android:layout_marginRight="@dimen/margin_for_seperator"
android:layout_marginStart="@dimen/margin_for_seperator"
android:gravity="end"
android:text="@string/seperator"
android:textColor="@android:color/white"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="start"
android:onClick="OnTermAndConditionsClicked"
android:text="@string/terms_and_conditions"
android:textColor="@android:color/white"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
My Landscape xml is --
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_page_back_gradient"
android:orientation="vertical"
android:fillViewport="true"
tools:context="com.thepsi.beintent.LoginPage"
android:id="@+id/parent_login_page">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_page_back_gradient"
android:orientation="vertical"
android:id="@+id/parent_container"
tools:context="com.thepsi.beintent.LoginPage">
<RelativeLayout
android:id="@+id/upper_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="@dimen/linear_right_20dp"
android:layout_marginRight="@dimen/linear_right_20dp"
android:layout_centerVertical="true"
android:layout_marginBottom="40dp"
android:layout_gravity="center">
<ImageView
android:id="@+id/img_logo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="@drawable/beintent_logo" />
<EditText
android:id="@+id/editTextLoginEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/img_logo"
android:layout_marginTop="@dimen/margin_between_views_login_page"
android:background="@drawable/box_edittext"
android:hint="@string/email_login"
android:inputType="textEmailAddress"
android:padding="10dp"
android:textStyle="bold" />
<EditText
android:id="@+id/editTextLoginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editTextLoginEmail"
android:layout_gravity="center"
android:layout_marginTop="12dp"
android:background="@drawable/box_edittext"
android:hint="@string/password_login"
android:inputType="textPassword"
android:padding="10dp"
android:textStyle="bold" />
<CheckBox
android:id="@+id/checkBoxLoginPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editTextLoginPassword"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textColor="@android:color/white"
android:textStyle="bold"
app:buttonTint="@android:color/white" />
<TextView
android:id="@+id/txt_check_box_lbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/checkBoxLoginPage"
android:layout_alignTop="@+id/checkBoxLoginPage"
android:layout_gravity="center_vertical"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/checkBoxLoginPage"
android:text="@string/remember_me_login"
android:textColor="@android:color/white"
android:textSize="@dimen/forgot_rember_text_size"
android:textStyle="bold" />
<TextView
android:id="@+id/txt_question_mark"
android:layout_width="24dp"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBoxLoginPage"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:background="@drawable/circle_background_red"
android:gravity="center"
android:text="@string/questionMark"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txt_question_mark"
android:layout_alignLeft="@+id/txt_check_box_lbl"
android:layout_alignTop="@+id/txt_question_mark"
android:layout_gravity="center_vertical"
android:layout_toRightOf="@id/txt_question_mark"
android:text="@string/forgot_password_login_page"
android:textColor="@android:color/white"
android:textSize="@dimen/forgot_rember_text_size"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_question_mark"
android:layout_marginTop="@dimen/margin_between_views_login_page"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btn_login"
android:layout_width="0dp"
android:layout_height="@dimen/button_height_loginpage"
android:layout_gravity="center"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@drawable/common_btn_selecter"
android:ems="10"
android:onClick="LoginButtonClicked"
android:text="@string/log_in_login_page"
android:textColor="#fff"
android:textSize="@dimen/login_page_button_text_size"
android:textStyle="bold" />
<Button
android:id="@+id/btn_sign_up"
android:layout_width="0dp"
android:layout_height="@dimen/button_height_loginpage"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:background="@drawable/common_btn_selecter"
android:ems="10"
android:onClick="SignUpButtonClicked"
android:text="@string/sign_up_login_page"
android:textColor="#fff"
android:textSize="@dimen/login_page_button_text_size"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/linear_layout_contact_us"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|bottom"
android:orientation="horizontal"
android:layout_below="@+id/upper_container"
android:paddingBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="end"
android:onClick="OnContactUsClicked"
android:text="@string/contact_us"
android:textColor="@android:color/white"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_for_seperator"
android:layout_marginLeft="@dimen/margin_for_seperator"
android:layout_marginRight="@dimen/margin_for_seperator"
android:layout_marginStart="@dimen/margin_for_seperator"
android:gravity="end"
android:text="@string/seperator"
android:textColor="@android:color/white"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="start"
android:onClick="OnTermAndConditionsClicked"
android:text="@string/terms_and_conditions"
android:textColor="@android:color/white"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
Might be helpful to someone.
<activity android:name=".SignUp"
android:configChanges="keyboardHidden|orientation|screenSize">
Add this line to all your activities to avoid crash with orientation changes. This will avoid the activity being loaded again and again on orientation changes.
For a little more information what you actually do with configChanges
here the link to the Android documentation.
Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.
Note: Using this attribute should be avoided and used only as a last resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.
And how to handle configuration changes
you can find here.
Use
import android.support.v4.app.Fragment;
instead of
import android.app.Fragment;
You might also need to change your activity to extend FragmentActivity instead of just Activity. More discussion Here.
android:configChanges="keyboardHidden|orientation"
add this in your manifest file in your activity.
add this in all your activities in Manifest
android:configChanges="keyboard|orientation|locale|fontScale|screenLayout|screenSize|uiMode|navigation|touchscreen|keyboardHidden|layoutDirection|smallestScreenSize"