Basically, i’m currently trying to have some buttons on a screen, when they are clicked it takes the user to a different activity with EditText field and a done button. Once the data is entered and done hit, it goes back and changes the name of the button text. I used this link to help me How to pass EditText to another activity?. the problem im having is once i click on a different button and enter the info for that and return it has forgotten the previous buttons data. I have a string so only the button clicked gets edited so i think it’s not storing data. I’ve spent ages trying different solutions to make it save state but nothing seems to be working.
I am quite new to Android so i’m well aware the solution may be very simple and i’ve just been implementing it wrong.
The code for the Activity with the buttons is like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.edit);
setupViews();
setupListeners();
mIntentString = savedInstanceState != null ?
savedInstanceState.getString("myKey"):null;
if(mIntentString == null){
Bundle extras = getIntent().getExtras();
mIntentString = extras != null ? extras.getString("myKey") : "Choose name";
}
if(ActivityController.button == "1"){
button1.setText(mIntentString);
Log.e(button, "button1 being checked");
}
...
@Override
public void onClick(View view){
if (view == button1) {
Intent i = new Intent();
i.setClass(ActivityEdit.this, ActivityEnterInfo.class);
startActivity(i);
}}
...
public void refreshButtons()
{
if(ActivityController.button == "1"){
mIntentString = button1.getText().toString();
Intent intent = new Intent();
intent.putExtra("returnKey",button1.getText().toString());
setResult(RESULT_OK,intent);
}
I hope this all makes sense, I’ve really hit a brick wall with this and need help
From my understanding, what you want is to save the state of your buttons internally and restore them when you get back.
You should override
onStart()oronPause()in your buttons activity to save the button data in private members, then overrideonStop()oronResume()and use it to restore your data.