Skip to content

TextView not working when I changing activity . #39

@ghost

Description

I want to add a message option to send message from server to client with this project.

I have two Activities: MainActivity and SecondActivity. On MainActivity I click on a button and go to SecondActivity.

public void msg(View view) {
    Intent intent = new Intent(this, secondActivity.class);;
    startActivity(intent);
}

SecondActivity

 public class SecondActivity extends AppCompatActivity {

   private TextView info;

  @Override
  protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_SecondActivity);
   
     info = findViewById(R.id.terv);


     new Thread(new Te()).start();

  }

  private class Te implements Runnable {
    @Override
    public void run() {
        while (true) {
            try {                 
                final String message = (String) MainActivity.objectInputStream.readObject();
                if (message != null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            info.append(message);
                        }
                    });
                }
        } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
         }
   }
 }
 }

The TextView works smoothly and shows the message when opening the SecondActivity for the first time.

But for the second time, when I go back to MainActivity and open the SecondActivity, the TextView doesn't work.

How can I solve this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions