我可以在我的第一個活動中初始化對象並且它在所有活動中都可以嗎?將對象傳遞給活動
public class Calc{
int x;
int y;
public Calc(int x, int y) {
this.x = x;
this.y = y;
}
public int sum() {
return x + y;
}
}
public class MainActivity extends Activity {
private int progressStatus = 0;
private Handler handler = new Handler();
private ProgressBar loading;
private static int progress;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Calc c = new Calc(3, 4);
}
}
public class PreviewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preview);
TextView txt = (TextView) findViewById(R.id.txt);
txt.setText(Integer.toString(c.sum));
}
}
如何初始化MainActivity中的Calc對象並使用它PreviewActivity。
我如何可以將它傳遞到另一個活動或我怎麼能使其與其他活動分享
爲什麼不直接在PreviewActivity中發送Calc的總和? – user370305