Home > Blockchain >  TextView sometimes loses contents on configuration change or Home button
TextView sometimes loses contents on configuration change or Home button

Time:03-22

A TextView is slowly filled with text coming from a background service. I have overridden the onSaveInstanceState() and onRestoreInstanceState() methods in MainActivity and added the android:freezesText="true" attribute to the TextView in the layout. In most cases the contents of the TextView are preserved when the device is rotated or the app is minimized, pressing the Home button, and another app is started. However, now and then the contents are lost. Since it is slowly collected by the background service it cannot be recovered easily and valuable information is lost.

I’m testing the app on a device with Android 7.0 but I also distributed it to other devices with different Android versions and it works fine, except for these occasional info losses.

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    static private boolean firstRun = true;

    private final BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                String newPost = bundle.getString(MyService.POST);
                textView.append((new Date()).toString().substring(11, 19)   " "   newPost   "\n");
                // Delete 250 leading chars if too many
                Editable ed = textView.getEditableText();
                int len = ed.length();
                if( len > 5000 ) ed.delete(0, 250);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.idTextView);
        if( firstRun ) {
            firstRun = false;
            Intent intent = new Intent(this, MyService.class);
            startService(intent);
        }
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle  savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putCharSequence("TEXTVIEW", textView.getText());
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        CharSequence viewContents = savedInstanceState.getCharSequence("TEXTVIEW");
        textView.setText(viewContents);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(receiver, new IntentFilter(MyService.NOTIFICATION));
    }
}

I've read a lot of posts about similar problems and I have done everything I know about to avoid it.

CodePudding user response:

you use shared preference

For gradle

implementation 'com.google.code.gson:gson:2.8.8'

Initialise the Shared Preference

SharedPreferences  mPrefs = getPreferences(MODE_PRIVATE);

Save data or any object and class

MyObject myObject = new MyObject;
Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(myObject);
prefsEditor.putString("MyObject", json);
prefsEditor.commit();

Get Data

Gson gson = new Gson();
String json = mPrefs.getString("MyObject", "");
MyObject obj = gson.fromJson(json, MyObject.class);

CodePudding user response:

check also Bundle passed as argument to onCreate method

@Override
protected void onCreate(Bundle savedInstanceState) {
    ... your code
    checkInstanceState(savedInstanceState); // at the end after GUI init
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    checkInstanceState(savedInstanceState);
}


private void checkInstanceState(Bundle savedInstanceState) {
    if (savedInstanceState==null) return;
    CharSequence viewContents = savedInstanceState.getCharSequence("TEXTVIEW");
    textView.setText(viewContents);
}
  • Related