Restoring state of TextView after screen rotation?
If you want to force your TextView
to save its state you must add freezesText
attribute:
<TextView
...
android:freezesText="true" />
From documentation on freezesText
:
If set, the text view will include its current complete text inside of its frozen icicle in addition to meta-data such as the current cursor position. By default this is disabled; it can be useful when the contents of a text view is not stored in a persistent place such as a content provider
TextView value doesn't restore when screen is rotated - Android
may be you somehow reload the activity parameters when going from portrait to landscape, which will accordingly resets counter
value to 0
again when rotating. you may try to make it static
variable to avoid conflicting.
private static int counter = 0;
Restoring a dialog after screen rotation
Running with the excellent comment by @MikeM, I realized the problem was that the RelativeLayout
of my activity had not yet been fully created in the onCreate()
method, and hence its width was not what I was expecting (likely to be zero).
As a workaround, I used getDisplayMetrics()
to access the actual device width, which does still exist at the point in the lifecycle where onCreate
gets called:
alertDialog = dialogBuilder.create();
alertDialog.show();
int width = (int)(getResources().getDisplayMetrics().widthPixels*0.90);
alertDialog.getWindow().setLayout(width, ViewGroup.LayoutParams.WRAP_CONTENT);
The lesson to be learned here is that there is a potential caveat when basing a dialog off something in your layout. In this case, when attempting to restore that dialog in onCreate
after device rotation would fail, but this is one possible workaround.
TextView contents is lost after changing screen orientation
Use this property in your TextView android:freezesText="true"
.
Why does LiveData not restore original UI state after screen rotation?
Found the answer to my question in this codelab: https://codelabs.developers.google.com/codelabs/android-lifecycles/#6
Some UI elements, including EditText, save their state using their own onSaveInstanceState implementation. This state is restored after a process is killed the same way it's restored after a configuration change.
TextView Disappears after Screen Rotation
Haha...after an hour of fiddling with it, i figured it out as soon as I finished asking the question. Thanks for your time though!
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putInt("cash", cash);
savedInstanceState.putInt("anteBet", anteBet);
savedInstanceState.putInt("playBet", playBet);
savedInstanceState.putInt("pairPlusBet", pairPlusBet);
savedInstanceState.putString("cashView", cashView.getText().toString());
// etc.
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
cash = savedInstanceState.getInt("cash");
anteBet = savedInstanceState.getInt("anteBet");
playBet = savedInstanceState.getInt("playBet");
pairPlusBet = savedInstanceState.getInt("pairPlusBet");
cashView.setText(savedInstanceState.getString("cashView"));
}
I changed
savedInstanceState.putString("cashView", cashView.toString());
to
savedInstanceState.putString("cashView", cashView.getText().toString());
and i changed
cashView.setText(savedInstanceState.getString(cashView.toString()));
to
cashView.setText(savedInstanceState.getString("cashView"));
Not sure which one fixed it, but its fixed. Thanks anyways!
TextView's text disappearing when device rotated
Please check on orientation change, on create method is called, which requires all the views to be created again, so you need to use one of the following methods:
- use onSavedInstance method and save the states of components/views to bundle.
Just use following flag true in your manifest file in activity tag android:configChanges="keyboardHidden|orientation". like below:
<activity android:name=".SampleActivity" android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation">
...
</activity>
Related Topics
Android Spanned, Spannedstring, Spannable, Spannablestring and Charsequence
Force Update of an Android App When a New Version Is Available
Recyclerview Inside Scrollview Not Scrolling Smoothly
How to Clear an Imageview in Android
Android Onclick in Xml VS. Onclicklistener
Opening Android Settings Programmatically
How to Customize Item Background and Item Text Color Inside Navigationview
Programmatically Getting the Gateway and Subnet Mask Details
Programmatically Relaunch/Recreate an Activity
Android: Resize Only Parts of View with Soft Keyboard on Screen
Read & Writing Arrays of Parcelable Objects
The Android Emulator Is Not Starting, Showing "Invalid Command-Line Parameter"
Error:Failed to Open Zip File. Gradle's Dependency Cache May Be Corrupt
Android Studio, Suddenly Got Gpu Driver Issue When Running Emulator
How to Block a Mobile Number Call and Message Receiving in Android Application Development