0

How do I get whatever is in text View to stay there when I close the activity and then reopen it?

public class ViewOffense extends Activity {
    EditOffense eo = new EditOffense();
    List<String> rosterLog;
    List<String> oneRow;
    Bundle bundle;
    static String selection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.viewoffense);

        bundle = this.getIntent().getExtras();
        if (bundle != null) {
            String selection = bundle.getString("key");
            TextView textview = (TextView) findViewById(R.id.textView2);
            textview.setText(selection);
        }
    }
}
Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • Possible duplicate of [Saving Android Activity state](http://stackoverflow.com/questions/151777/saving-android-activity-state) – boxed__l Jul 31 '16 at 11:54

5 Answers5

1

Store the textview value in shared preferences when you destroy the activity.

    @Override
        public void onDestroy() {
            super.onDestroy();
TextView tvText = (TextView) findViewById(R.id.YOURLAYOUTGOESHERE);
            SharedPreferences.Editor prefEditor = getSharedPreferences("Preferences", Context.MODE_PRIVATE).edit();
            prefEditor.putBoolean("text", tvText .getText().toString());
            prefEditor.commit();
        }

Then in onCreate set the textview text from your shared preferences.

TextView tvText = (TextView) findViewById(R.id.YOURLAYOUTGOESHERE);
    SharedPreferences prefs = getSharedPreferences("Preferences", Context.MODE_PRIVATE);
    if (prefs.contains("text")){
            tvText .setText(prefs.getString("text", ""));
        }
dues71
  • 291
  • 1
  • 7
  • I would recommend going with this solution. Works flawlessly! – Si8 Oct 30 '13 at 17:25
  • I am getting an error message under TextView.getText(). Cannot make a static reference to the non-static method getText() from the type TextView – pdmadd8504 Oct 30 '13 at 17:52
  • You have to declare the textview earlier in the code. For example TextView tvText = (TextView) findViewById(R.id.layout); Then use tvText.getText().toString() – dues71 Nov 19 '13 at 20:45
0

step 1 : create a bean with the values you want to store of the activity

step 2 : create the bean instance and store activity values in the instance

step 3 : keep the instance passing until u need the state to be active

step 4 : when get back to activity , initialize the activity with bean instance values

0

You should explore the various storage options available in Android to persist data between launches of your application.

For storing the content of a single TextView, using SharedPreferences would likely suffice.

You would likely want to store the value in onStop() and retrieve it in onCreate(). For example:

public class ViewOffense extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";

    @Override
    protected void onCreate(Bundle state){
       super.onCreate(state);

       // Restore String
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       String savedText = settings.getString("myText", "default text");
       TextView textview = (TextView) findViewById(R.id.textView2);
       textview.setText(savedText);
    }

    @Override
    protected void onStop(){
        super.onStop();

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();

        TextView textview = (TextView) findViewById(R.id.textView2);
        String text = textview.getText().toString();
        editor.putString("myText", text);

        // Commit the edits!
        editor.commit();
    }
}
Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • Worth mentioning: if keeping data in memory is enough, it is possible to use a static variable or, more appropriately, keep it in a subclass of `Application`. The data won't persist throughout multiple executions of the app though. – Piovezan Oct 30 '13 at 17:17
  • OP specifically asked about keeping it after closing the Activity. I wouldn't assume that anything stored in memory will still be around when the Activity is re-created. – Bryan Herbst Oct 30 '13 at 17:18
  • OP didn't mention closing the app, but the activity. – Piovezan Oct 30 '13 at 17:18
  • In either case, assuming that data will still be in memory is a bad idea. – Bryan Herbst Oct 30 '13 at 17:19
0

Store current value of the TextView to outState in onSaveInstanceState (Bundle outState). Then in onRestoreInstanceState (Bundle savedInstanceState) or onCreate (Bundle savedInstanceState), the value can be retrieved from the savedInstanceState.

huy.nguyen
  • 454
  • 2
  • 9
  • And don't save the value to `SharedPreferences`. It's persistent and is not designed for this task. – huy.nguyen Oct 30 '13 at 17:23
  • Could you show me in code I don't have a grasp on this concept? – pdmadd8504 Oct 30 '13 at 18:08
  • Note that even when your background activity (or sometimes your app process if the app is in background) is killed by the system and user later goes back to your app, a new activity will be create and it will receive the savedInstanceState the previous activity had generated. So this is the correct way to store dynamic instate state of your activity, which is not supposed to be persistent. http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29 – huy.nguyen Oct 30 '13 at 18:38
0

store the values in the onSaveInstanceState(Bundle) method.

String value;
yourbundle.putString("key",value);

the values you store in the onSaveInstanceState will be available to you as parameter passed to your onCreate/onRestoreInstanceState method by default ie.Bundle savedInstanceState all you need to go is get the value from savedInstanceState as

String data = savedInstanceState.getString("key defined earlier");

and set it to your textview

this is a solution for temporary data. for persistent storage use preferences or write it to a sqlite database.

Niraj Adhikari
  • 1,678
  • 2
  • 14
  • 28