Jayizzle
Jayizzle

Reputation: 532

Android changing the TextView from another activity

I have two activities where the main one has a TextView which can be altered in a sharedPreference in the second activity. When the user wants to change or "saves" the string, it saves it in the SP file, and returns back to the main activity. However, the TextView DOES not change to the new one and shows the old one. The app needs to restart for it to work.

My goal was to use the activity lifestle upon the systems onResume but that didn't pick up the new string at all.

I am asking how to change the TextView upon saving/returning from the second activity. The line in question is: checkboxmessage = (sp.getString("checkboxdescr", ""));

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    //
    //  sp= PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); // forget about
    sp = getSharedPreferences("contactapp", 0);
    // named preferences - get the default ones and finish with it


    //SET TEXTS


    smsintroduction = (sp.getString("intro", ""));
    smsbody = (sp.getString("body", ""));
    checkboxtext = (sp.getString("checkbody", ""));
    checkboxmessage = (sp.getString("checkboxdescr", ""));

    TextView tv = (TextView) findViewById(R.id.sexycheckbox);
    tv.setText(checkboxtext);

    CheckBox cb = (CheckBox) findViewById(R.id.sexycheckbox);

    ////TOPCLEAR
    TextView tt = (TextView)findViewById(R.id.toptext2);
    tt.setOnClickListener(new View.OnClickListener() { 
        @Override
        public void onClick(View v) {

            EditText et = (EditText)findViewById(R.id.edit_name);
                    EditText et2 = (EditText)findViewById(R.id.edit_number);
                    et.setText("");
                    et2.setText("");
                    CheckBox sexycheck;
                     sexycheck = (CheckBox)findViewById(R.id.sexycheckbox);
                    if (sexycheck.isChecked()) {
                        sexycheck.setChecked(false);
                    }
        }
    });

}

protected void onResume(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    sp = getSharedPreferences("contactapp", 0);

    smsintroduction = (sp.getString("intro", ""));
    smsbody = (sp.getString("body", ""));
    checkboxtext = (sp.getString("checkbody", ""));
    checkboxmessage = (sp.getString("checkboxdescr", ""));

    TextView tv1 = (TextView) findViewById(R.id.sexycheckbox);
    tv1.setText(checkboxtext);
}

Upvotes: 1

Views: 157

Answers (2)

Gustek
Gustek

Reputation: 3760

Even when You copy paste You still need to know what You coping.

replace theses lines

super.onCreate(savedInstanceState); //You call wrong parent method here
this.requestWindowFeature(Window.FEATURE_NO_TITLE); //you call this in onCreate already
setContentView(R.layout.activity_main); //you call this in onCreate already

sp = getSharedPreferences("contactapp", 0); //you call this in onCreate already

with

super.onResume(savedInstanceState);

in onResume() method.

Upvotes: 1

Ultimo_m
Ultimo_m

Reputation: 4897

Change your code like this and see if it works:

@Override
protected void onResume() {

    super.onResume();

    tv1.setText("new Text test");
}

Upvotes: 1

Related Questions