Android – changing textview from other activity

Issue

I’m quite new to programming.

I’m trying to make a simple app with two activities, where the second activity can change the text of the first. I know it can be done using intents, but I was wondering if there is a more direct way of doing it, for example using the second activity to call a function from the first activity?

Here’s the code I have so far:

The MainActivity, which contains a TextView and a button to open the second activity:

public class MainActivity extends Activity {

TextView textview;
Button button;

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

    textview = (TextView) findViewById(R.id.et2);
    button = (Button) findViewById(R.id.b1);

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainActivity.this, ChangeText.class);
            startActivity(intent);
        }
    });
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
}

public void changetext(String message) {
    textview.setText(message);
}
}

And the second activity, ChangeText, which contains an EditText and a button which should change the text of the TextView in MainActivity and then finish itself:

public class ChangeText extends Activity{

EditText edittext;
Button button;
private MainActivity mainclass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.change_text);

    edittext = (EditText)findViewById(R.id.et2);
    button = (Button)findViewById(R.id.b2);

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        String message = edittext.getText().toString(); 
        mainclass.changetext(message);
        finish();
        }
    });
}
}

As you can see I tried to make the app work by making a public function in MainActivity which receives a string and sets the TextView with it, and then I call this function from the ChangeText activity.

The problem: it keeps crashing! Can anyone tell me how I can make this work?

Solution

Seems like I answered almost exactly the same question a week or so ago, so this is probably a duplicate, but I can’t seem to find the original question.

The short answer is no – you can’t call a method in an Activity from another Activity. The issue is that for normal programming purposes, only one Activity exists at a time*.

If you do something to circumvent this, then you’re risking causing some major issues, including high memory usage, null pointer exceptions, etc.

The correct way to do this is indeed through the Intent system.

* Activities may or may not actually get destroyed when they become inactive, depending on things like how you use the back stack.
However, you should always program is if they do get destroyed when they become inactive – read, understand, and respect the Activity lifecycle.

Answered By – GreyBeardedGeek

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *