1

In Activity A, I can view a string. There is an EDIT button I press in Activity A that starts Activity B (and closes A). I pass the string with an intent from A to B in my onCreate code like this:

In Activity A I have:

//for the edit button
edit = (Button) findViewById(R.id.edit);

edit.setOnClickListener(new View.OnClickListener() {
  public void onClick(View view) {

//open the Edit Activity, pass over the string
  Intent i = new Intent(ViewContact.this, EditContact.class);
  i.putExtra("category", categoryname.getText());

//start Activity B
  startActivity(i);

//close Activity A
  finish();

  }
});

In Activity B I have:

Intent i = this.getIntent();
category = i.getStringExtra("category");
//set the EditText to display the value of "category" key
categoryname.setText(category);

All works so far. But if my back button is pressed in Activity B to go back to A, I want to show the string again. As it stands, there is no value in the text box.

Back button code in Activity B:

//for the back arrow, tell it to close the activity, when clicked
ImageView backButton = (ImageView) logo.findViewById(R.id.back_arrow_id);
backButton.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {

    Intent i = new Intent(getApplicationContext(), ViewContact.class);
    i.putExtra("category",  categoryname.getText());

    //Start Activity A
    startActivity(i);

    //Finish Acitivity B
    finish();
  }
});

And then when Activity A starts again it should call the intent in onCreate with

 Intent i = this.getIntent();
category = i.getStringExtra("category");
categoryname = (TextView) findViewById(R.id.textViewCategory);
categoryname.setText(category);

But instead the text box is empty.

Can you to tell me how I should go about passing the value back from B to A?

CHarris
  • 2,693
  • 8
  • 45
  • 71
  • Are you sure that `categoryname.getText())` in Activity B returns something? The code you have looks just fine. There are better ways to do this though, like using `startActivityForResult()`. But what you have should work. – David Wasser Jul 03 '18 at 13:33
  • Yes, it returns something. Really not sure why it's not working, will post answer when I do find it. Activity A does itself get the string with an intent from another activity, will check if there's a mix up somewhere. I'm also using AsyncTask, at present seeing if it works in onPostExecute instead of onCreate... – CHarris Jul 03 '18 at 14:09
  • @DavidWasser For some reason I can't post an answer to my own question...anyway, not too sure why my textbox was always empty; yes there were 2 intents being called in the activity but don't understand why one of the values was being put into categoryname rather than none at all...in any case I did this and now it works going back to the activity: `if (categoryname.getText().toString().trim().length() == 0) {categoryname.setText(stuffhere)}` – CHarris Jul 04 '18 at 13:07
  • 1
    You cant post an answer because the question has been closed as a duplicate. Glad you were able to solve the problem. – David Wasser Jul 04 '18 at 14:48

1 Answers1

1

Just delete the finish() command

masoud vali
  • 1,528
  • 2
  • 18
  • 29