2012-06-15 41 views
3

我正在从事一个项目,并让我的活动通过意图传递了其他信息。不过,我最近决定开发并使用Singleton方法,并拥有一个保存我所有数据的Application类。不使用意图转换活动

我现在有点困惑:我如何开始我的活动没有意图?这是我之前拨打过的一个电话...

public boolean onOptionsItemSelected(MenuItem item) { 
    switch(item.getItemId()) { 
    case R.id.connect: 
     startActivityForResult(new Intent(this, DeviceList.class), REQUEST_CONNECT_DEVICE ); 
     return true; 

我该如何更改它来启动DeviceList类而不使用意图?

+1

我不认为你可以开始一个活动没有意图。从文档中我没有记住 –

回答

9

你没有。使用意图开始你的活动。这就是Android的设计。

+0

啊,这对我来说更有意义。我曾认为Intent只是为了传递数据,不一定是这些活动是如何开始的。谢谢。 – JuiCe

+1

@ user1435712:阅读并理解以下链接将非常有帮助。 http://developer.android.com/guide/topics/intents/intents-filters.html – Squonk

+2

除了链接到文档@Squonk,您可能还需要阅读[应用程序基础知识文档](http:// developer .android.com/guide/topics/fundamentals.html),尤其是[激活组件]部分(http://developer.android.com/guide/topics/fundamentals.html#ActivatingComponents)。 – kabuko

1

两种活动之间传递的数据不仅是意图的侧倾..它passs另一个必须需要infomration这是必须的,用于创建一个活动像Context,包信息等等

按照deination如link

意图是一个bstract description of an operation to be performed

3

是的,你可以。首先,你需要扩展ActivityGroup

public class Activity1 extends ActivityGroup 

然后u需要在类中创建一个方法来替代内容视图,在这种情况下Activity1

public void replaceContentView(String id, Intent newIntent) { 
    View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); 
    setContentView(view); 
} 

当你想从活动1开始活性2 ,

Intent activity2 = new Intent(this, Activity2.class); 
replaceContentView(Activity2.class.getName(), activity2); 

你甚至可以从活性2

开始Activity3
Intent activity3 = new Intent(getParent().getApplicationContext(), Activity3.class); 
Activity1 parent = (activity3) getParent(); 
parent.replaceContentView(Activity3.class.getName(), activity3); 

希望这可以帮助

P.S:您可以参考this link

+0

为什么在方法参数中使用Intent?如下所示:public void replaceContentView(String id,Intent newIntent) –