Home
› Uncategorized
As mentioned in the post "Activity will be re-started when screen orientation changed", onCreate() method will be called when orientation changed, to re-layout the screen. But in case of some views, such as EditText, the Android life-cycle mechanism will restore the states.
Here is a example, there are two EditText in the layout. The first one is assigned with a ID, it will update the TextView once text changed. The second one have no ID assigned. You can notice that the text in the first EditText (with ID) will keep no change after orientation changed, and onTextChanged() method of the TextChangedListener will be called also, to update the TextView.
On the other hand, the second EditText (without ID assigned) will clear to empty when orientation changed.
Layout
Next:
- Retrieve old activity state for configuration change by overriding onRetainNonConfigurationInstance() method
Here is a example, there are two EditText in the layout. The first one is assigned with a ID, it will update the TextView once text changed. The second one have no ID assigned. You can notice that the text in the first EditText (with ID) will keep no change after orientation changed, and onTextChanged() method of the TextChangedListener will be called also, to update the TextView.
On the other hand, the second EditText (without ID assigned) will clear to empty when orientation changed.
Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" tools:context=".MainActivity" /> <TextView android:id="@+id/lasttext" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:context=".MainActivity" /> <EditText android:id="@+id/intext" android:layout_width="match_parent" android:layout_height="wrap_content" tools:context=".MainActivity" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" tools:context=".MainActivity" /> </LinearLayout>
package com.example.androidorientationchange; import android.os.Bundle; import android.app.Activity; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { TextView lastText; EditText inText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lastText = (TextView)findViewById(R.id.lasttext); inText = (EditText)findViewById(R.id.intext); inText.addTextChangedListener(new TextWatcher(){ @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub lastText.setText(s); Toast.makeText(MainActivity.this, "onTextChanged: " + s, Toast.LENGTH_SHORT).show(); }}); Toast.makeText(MainActivity.this, "onCreate", Toast.LENGTH_LONG).show(); } }
Next:
- Retrieve old activity state for configuration change by overriding onRetainNonConfigurationInstance() method
-- Delivered by Feed43 service
Related Posts
There is no other posts in this category.Popular
-
[Android] The Dark Knight Rises v1.1.1 APK by GameloftThe Dark Knight Rises is one of the hottest games just released by Gameloft in July 2012 on Androi…
-
The Avengers – Iron Man Mark VII 1.1 APKThe Avengers – Iron Man Mark VII 1.1 APK Suit up and Blast off in the First-Ever Fully-Interactive …
-
Angry Birds Space v1.0.1 for AndroidAngry Birds Space v1.0.1 for Android The #1 mobile game of all time blasts off into space! All new …
-
Ripple Lock v1.6Ripple Lock v1.6 Requirements: for Android version 2.1 and higher Overview: Is still envy of the br…
-
How To: Copy And Paste In Android Mobile'>The copy, cut, paste and select the features Android has many nuances. In this how-to, we will foc…
-
Tap Sonic (Love Ritmo Theme) New APK + songsThis is new TapSonic APK cracked by Wolfhack (so credit goes to him), this new APK (based on 1.0.6 …
-
Facebook for Android v1.8.3Facebook for Android v1.8.3 Requires Android:1.5 and up Share status updates from your home screen,…
Post a Comment
Post a Comment