2011-12-30 50 views
3

比方說,我想哪個是啓動活動的正確方法?

我以前使用這種方法來啓動活動啓動名爲「OccupyThePieShop」活動:

Intent oTPS = new Intent(); 
timeIntervalConfigIntent.setClassName("com.aXX3AndSpace.KeepInTouch", 
    "com.aXX3AndSpace.KeepInTouch.OccupyThePieShop"); 
startActivity(oTPS); 

...但被告知,這是更規範:

Intent oTPS = new 
    Intent(KeepInTouchActivity.this, OccupyThePieShop.class); 
KeepInTouchActivity.this.startActivity(oTPS); 

...所以我用這個用法替換了我的電話startActivity()。現在

,我遇到一對夫婦更哪種方式顯得相當 「優雅」,即:

startActivity(new Intent(getApplicationContext(), OccupyThePieShop.class)); 

...和:

Intent intent = new Intent(this, OccupyThePieShop.class); 
startActivity(intent); 

是比其他優選的一種方式如果是這樣,爲什麼?

回答

2

我認爲這可能是個人喜好的問題。我喜歡startActivity(new Intent(this, OccupyThePieShop.class));,因爲如你所說,它很優雅。

+1

當你知道你爲什麼需要它時,只能使用'getApplicationContext()'。你從不需要'startActivity()'。請使用'this'。 – CommonsWare 2011-12-30 00:55:40

+0

好的。 (更多的字符,所以SO會讓我發佈) – BenH 2011-12-30 00:57:03

+0

那麼,我的「elegent」方式不編譯。用這一行: startActivity(new Intent(this,ContactsListListActivity.class)); 我得到的, 「構造意圖(新View.OnClickListener(){},類)是未定義」 – 2011-12-31 03:48:44

相關問題