Search code examples
androidandroid-activityactivity-lifecycle

How do I display public text in Activity A after it has been changed by Activity B


This probably demonstrates the most appalling lack of understanding of the activity life cycle, but please be sympathetic. I am ultimately going to want to invoke Activity B from Activity A a number of times, each time passing a different parameter to Activity B which is then responded to by the user and stores/sets various public variables. As a precursor to this, I just want to get my head round how Activity A sees the change to a public variable that Activity B has changed.

I have three very simple classes: Common.java that holds the public variables, the main activity MainActivity.java and the child activity Child.java. There is only one public variable right now; it's the string mess1 which is initialized to "***". All the code does at the moment is when mainbutton is clicked in MainActivity, it invokes Child. In Child, we immediately set mess1 to "Child here" then set the text in a Child-based TextView to mess1. On clicking the childbtn button in Child, we finish() the child activity (and of course the system returns us to MainActivity.

When this app is run, wee see the three stars displayed in MainActivity. When mainbutton is pressed we go to Child and see "Child here" displayed. When the childbtn is pressed, we return to MainActivity BUT, the three stars are still there although we know for sure that mess1 now holds "Child here".

My questions are: 1. Why, when we know mess1 has been changed, does MainActivity still display "***" on return from the Child activity? 2. What do I need to change in the code to get "Child here" to display?

Relevant code extracts follow. Thanks in advance for your help.

Common.java

public class Common
{
    public static String mess1 = "***";
}

MainActivity.java

public class MainActivity extends AppCompatActivity
{

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

    Button mainbutton = (Button)findViewById(R.id.mainbutton);
    TextView maintop = (TextView)findViewById(R.id.maintop);

    mainbutton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            startActivity(new Intent(MainActivity.this, Child.class));
        }
    });

    maintop.setText(Common.mess1);
}

Child.java

public class Child extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_child);

    TextView childtext = (TextView) findViewById(R.id.childtext);
    final Button childbtn = (Button) findViewById(R.id.childbtn);

    Common.mess1 = "Child here";
    childtext.setText(Common.mess1);

    childbtn.setOnClickListener
            (new View.OnClickListener()
             {public void onClick(View v)
                 {finish();
                 }
             }
            );
}

Solution

  • You should set the text in onResume() of MainActivity. When you get back from Child.java onResume() (not onCreate()) is invoked and, since maintop's text is set in onCerate() only, nothing changes it on return.

    protected void onResume() {
         super.onResume();
    
         maintop.setText(Common.mess1);
     }
    

    Reference: Activity Lifecycle and Implementing the lifecycle callbacks