简体   繁体   English

intent.putExtra做什么?

[英]What does intent.putExtra do?

i just started learning android development and a bit confused about what intent.putExtra does. 我刚刚开始学习android开发,对intent.putExtra的作用有点困惑。 Can somebody please explain this? 有人可以解释一下吗?

Thanks 谢谢

An Android application can contain zero or more activities. Android应用程序可以包含零个或多个活动。 When your application has more than one activity, you often need to navigate from one to another. 当您的应用程序有多个活动时,您通常需要从一个活动导航到另一个活动。 In Android, you navigate between activities through what is known as an intent. 在Android中,您可以通过所谓的意图在活动之间导航。 By using putExtra(), you can pass some information to the activity you are intending to start. 通过使用putExtra(),您可以将一些信息传递给您打算启动的活动。 eg: 例如:

//code inside the recent activity, from where you want to start another activity //最近活动中的代码,您要从哪里开始另一个活动

Intent myIntent = new Intent(this, SecondActivity.class); 
myIntent.putExtar("name","xx");
myIntent.putExtra("age",30);

startActivity(myIntent);

// code inside another activity(SecondActivity), which you are starting using intent //代码在另一个活动(SecondActivity)中,您开始使用intent

Bundle resultIntent = getIntent().getExtras();

if(resultIntent != null)
 {  
  String nameValue = resultIntent.getString("name"); 
  int ageValue = resultIntent.getInt("age");
 }

When you start a new Intent, you may want to pass some information to it. 当您启动新的Intent时,您可能希望将一些信息传递给它。 putExtra is how you so that. putExtra就是这样的。 For instance if you made an intent to display account details, you might pass it the account id that is to be displayed. 例如,如果您打算显示帐户详细信息,则可以将其传递给要显示的帐户ID。 Basically any information you put in the extra bundle can be read later by the intent you've given it to. 基本上,您在额外包中放入的任何信息都可以通过您给出的意图稍后阅读。

Intent.putExtra

Is used to provide more info to a component. 用于向组件提供更多信息。 For example an intent to send an email, you would use it to supply extra info such as subject and body. 例如,发送电子邮件的意图,您将使用它来提供额外的信息,如主题和正文。 For more on Intents see Android Developers 有关Intents的更多信息,请参阅Android开发者

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM