2011-12-30 109 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

相关问题