Reputation: 53
I'm pretty new to Android Development and I've come across a problem with my TextView. I have an XML file that contains a ScrollView and a TextView:
<ScrollView
android:id="@+id/scroll1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:freezesText="true">
</TextView>
</ScrollView>
And I have included it in two different XML files
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<include
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/story_view"
layout="@layout/story_view" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/add_text">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:text="@string/end_button"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:onClick="endButtonPressed">
</Button>
<Button android:text="@string/submit_button"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="textAdded">
</Button>
</LinearLayout>
and
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/story_view" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:text="@string/save"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:onClick="saveToDevice">
</Button>
<Button android:text="@string/facebook"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:onClick="saveToFacebook">
</Button>
</LinearLayout>
But when I go from the first XML file to the other (and changing the Activity in the process), the content of the TextView disappears. I have tried freezesText but that doesn't seem to work.
Ordinarily I would just pass the content in an intent but my text is in different colours and I want to maintain that.
I could pass a Bitmap image in an intent but I want to avoid that if possible.
Thanks.
Upvotes: 1
Views: 3225
Reputation: 749
You could use the Activity state to save some values like
@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.putBoolean("MyBoolean", true);
savedInstanceState.putDouble("myDouble", 1.9);
savedInstanceState.putInt("MyInt", 1);
savedInstanceState.putString("MyString", "Welcome back to Android");
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
double myDouble = savedInstanceState.getDouble("myDouble");
int myInt = savedInstanceState.getInt("MyInt");
String myString = savedInstanceState.getString("MyString");
}
Upvotes: 3
Reputation: 8616
That happen because when you come back to the first activity, the onCreate method is called, so your textView is a new textView. Take a look to Activity Lifecycle, here is explained much better.
You can use sharedPreferences or a Singleton (a design pattern), where the class can be instantiate only one time:
public class MySingleton{
private static MySingleton INSTANCE = null;
private String textViewInformation;
private MySingleton() {
}
public static MySingleton getInstance(Context context) {
synchronized (MySingleton.class) {
if (INSTANCE == null) {
synchronized (MySingleton.class) {
if (INSTANCE == null)
INSTANCE = new MySingleton();
}
}
}
return INSTANCE;
}
public String getTextViewInformation(){
return textViewInformation;
}
public void setTextViewInformation(String textViewInfo){
textViewInformation = textViewInfo;
}
}
and then:
public void onDestroy() {
super.onDestroy();
MySingleton.getInstance(this).setTextViewInformation("textViewText");
}
public void onResume() {
super.onResume();
if(MySingleton.getInstance(this).getTextViewInformation() != null){
yourTextView.setText(MySingleton.getInstance(this).getTextViewInformation());
}else{
yourTextView.setText("new text");
}
Maybe this way it's longer than the shared preferences, but it's very useful. Excuse my bad english! I hope this help.
Upvotes: 0
Reputation: 8853
you can use the sharedpreferences
for this purpose it allows you to store and retrieve data using key-value pairs
. for satisfying your purpose you need to store your activity state in sharedpreferences
.
particularly in your case first you need to store the textview value in shared preferences when you destroy the activity
@Override
public void onDestroy() {
super.onDestroy();
TextView tvText = (TextView) findViewById(R.id.yourelement);
SharedPreferences.Editor prefEditor = getSharedPreferences("Preferences", Context.MODE_PRIVATE).edit();
prefEditor.putBoolean("text", tvText .getText().toString());
prefEditor.commit();
}
Then in onCreate
you need to set the textview text from your shared preferences.
TextView tvText = (TextView) findViewById(R.id.yourelement);
SharedPreferences prefs = getSharedPreferences("Preferences", Context.MODE_PRIVATE);
if (prefs.contains("text")){
tvText .setText(prefs.getString("text", ""));
}
Upvotes: 0