Chris
Chris

Reputation: 3817

Android Parent and Child Activity onCreate question

I have a parent activity, and a child activity that extends the parent activity. When the parent starts the child activity,

Which onCreate gets executed first? The child's or parent's?

There is a particular variable I am setting in the Child activity's onCreate method, and right now, it looks like it takes a while to get to the Child activity's onCreate, and so the methods in the Parent are reporting an empty variable. Whereas when I make the Parent sleep for a while, it reports the correct variable.

Thanks Chris

Parent Activity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mylayout);

    goButton = (ImageButton) this.findViewById(R.id.goButton);
    goButton.setOnClickListener(new OnClickListener() {
        public void onClick(View view) {

            Intent childIntent = new Intent("com.example.Child");

            String newValue = "Child Value";
            Bundle bun = new Bundle();
            bun.putString("value", newValue); // add two parameters: a string and a boolean
            childIntent.putExtras(bun);
            startActivity(childIntent);
        }
    });

    this.doTheWork("Parent Value");
}

private void doTheWork(String value) {
    new MyNewThread(value).start();
}


public String getTheValue(String value) {
    return "My Value is: " + value;
}

private class MyNewThread extends Thread {
    String value;
    public LoadThread(String v) {
        this.value = v;
    }
    @Override
    public void run() {
        String str = getTheValue(this.value);
    }
}

Child Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle bun = getIntent().getExtras();
    childValue = bun.getString("value");
}

public String getTheValue(String value) {

    return "My Value is: " + value;
}

So, basically, even after the Parent starts the Child, it still returns "Parent Value", but when I have the thread sleep, it return "Child Value".

Upvotes: 3

Views: 3374

Answers (2)

Submersed
Submersed

Reputation: 8870

Your extended class's method (the child class) is called first, but your super class's method is called immediately after since the first line in your child class's method is

Child Activity

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

This is an explicit call to your super class's onCreate Method.

Upvotes: 2

Hsd
Hsd

Reputation: 1

First answer about the sequence. First is call parent and next one is child. This information you can read from your source code.

Next information is about why you have different result when you are using sleep. First of all the code of Child Activiry onCreate is not completed because is not show how you are using field (or local variable) childValue. But I expect that you are creating new thread in child object too. Please remember that thread are not run in the moment when you are creating the thread but in the moment when the runtime find time for this. The method sleep inform runtime that you can run the thread because your main thread is sleeping and this is the reason of different result. Important is that you are creating two thread one in child and one in parent.

Upvotes: 0

Related Questions