Android 父子 Activity onCreate 问题
我有一个父活动和一个扩展父活动的子活动。当父 Activity 启动子 Activity 时,
哪个 onCreate 首先执行?孩子的还是父母的?
我在子活动的 onCreate 方法中设置了一个特定的变量,现在看起来需要一段时间才能到达子活动的 onCreate 方法,因此父活动中的方法报告了一个空变量。而当我让父级休眠一段时间时,它会报告正确的变量。
谢谢 克里斯
父活动:
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);
}
}
子活动:
@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;
}
所以,基本上,即使父启动子之后,它仍然返回“父值”,但是当我让线程睡眠时,它返回“子值”。
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".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先调用扩展类(子类)的方法,但随后立即调用超类的方法,因为子类方法中的第一行是
Child Activity
这是对超类的 onCreate 方法的显式调用。
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
This is an explicit call to your super class's onCreate Method.
首先回答有关顺序的问题。第一个是调用父级,下一个是子级。您可以从源代码中读取此信息。
下一个信息是关于为什么在使用睡眠时会产生不同的结果。
首先,Child Activiry onCreate 的代码未完成,因为没有显示您如何使用字段(或局部变量)childValue。但我希望您也在子对象中创建新线程。请记住,线程不是在您创建线程的那一刻运行,而是在运行时找到时间的那一刻运行。 sleep 方法通知运行时您可以运行该线程,因为您的主线程正在休眠,这就是不同结果的原因。重要的是,您要创建两个线程,一个在子线程中,一个在父线程中。
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.