我的意思是,代碼應該幾乎相同,我需要這兩個活動。根據所調用的活動,可能需要一些ifs。
如果他們只是根據您調用的內容和方式進行少量更改,則可以通過使用Intent向其傳遞值來使用相同的Activity類。假設你正在通話中 「CommonActivity」 爲 「A」 型,然後同時呼籲 「CommonActivity」 使用:
Intent intent = new Intent(this, CommonActivity.class);
intent.putExtra("calledForType", "A");
在你CommonActivity:
String calledForType = "";
public void onCreate(Bundle savedInstanceState) {
......
calledForType = this.getIntent().getStringExtra("calledForType");
calledForType = calledForType.toUpper(); // Better for comparing later
}
現在,無論你必須處理與如果公司的differnces使用:
if (calledForType.equals("A")) {
// Do This For A
} else if (calledForType.equals("B")) {
// Do this for B
}
// Do this for common
這樣,您可以處理N個活動。
來源
2012-02-03 14:33:24
Tvd
我對國家模式有點困惑,究竟是什麼對我的情況有好處(也許我真的沒有得到它的目的)。 – 2012-02-03 17:41:28