可能太简单了,但我找不到正确的方法。
在 C++ 中,我可以编写initWithParameter: xxx
实例化一个类,然后在 init 中设置一些实例变量,给定 init 时的值。
在Java中我不知道该怎么做。目前我执行以下操作:
public class SpecialScreen extends BASEScreen{
private static final int ACTIVITY_1 = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //create the instance
defineScreenType (ACTIVITY_1); //second call to set the instance variable
presentOnScreen();
}
在 BASEScreen 中时:
public class BASEScreen extends Activity {
private Integer activityCode; // which activity should I do?
@Override
public void onCreate(Bundle savedInstanceState) { // the creation
super.onCreate(savedInstanceState);
}
// the setting of the instance variable
public void defineScreenType(int screenID) {
activityCode = screenID;
}
这不可能是最好的方法。如何更好地做到这一点?
谢谢
添加以显示在 BASEScreen 中调用 SpecialScreen:
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
Intent i;
switch (item.getItemId()) {
case OTHER_PAGE_ID:
//
if (activityCode == ACTIVITY_1) {
i = new Intent(this, SpecialScreen2.class);
i.putExtra("Task", ACTIVITY_2);
startActivityForResult(i, ACTIVITY_2);
finish();
} else {
i = new Intent(this, SpecialScreen1.class);
i.putExtra("Task", ACTIVITY_1);
startActivityForResult(i, ACTIVITY_1);
finish();
}
return true;
ps 我知道不再需要放置 Extra 了。这是我在拥有两个 SpecialScreen 子类并始终使用此参数调用 BASEScreen 之前的方式。