简体   繁体   English

使用意图在活动之间传递字符串值

[英]Passing String Values between Activities using intents

I understand how to pass string values from the MainActivity to the SecondaryActivity using intents, but how would you do that Vise Versa. 我了解如何使用意图将字符串值从MainActivity传递到SecondaryActivity,但是您将如何实现Vise Versa。 Here is the code that I am using, I just don't know where to put the Recieving classes code. 这是我正在使用的代码,我只是不知道将接收类代码放在哪里。

In my SecondActivity 在我的SecondActivity中

Intent intent = new Intent(SecondActivity.this, MainActivity.class);
String TimeValue = ("00:00:00");
intent.putExtra("TimeValue", TimeValue);
startActivity(intent)

and This is the code that I am not sure where to put so it doesn't crash when the app starts 这是我不确定将代码放在哪里的代码,因此在应用启动时不会崩溃

String intent = getIntent().getExtras().getString("TimeValue");
TextView timeText = (TextView)findViewById(R.id.timeText);
timeText.setText(intent);

The problem is that MainActivity won't always be created with an intent coming from SecondActivity . 问题是MainActivity不会总是以SecondActivity的意图创建的。 It will also be created just when the app is launched. 它也将在启动应用程序时创建。

You need to make sure that extras actually exists before trying to get extras from it! 在尝试从中获取附加功能之前,您需要确保extras确实存在! It could be null! 可能为空!

So this should be in your onCreate method, after you inflate the view. 因此,应在为视图充气后将其放在onCreate方法中。

Bundle extras = getIntent().getExtras();

String intentString;
if(extras != null) {
    intentString = extras.getString("TimeValue");
} else {
    intentString = "Default String";
}
TextView timeText = (TextView)findViewById(R.id.timeText);
timeText.setText(intentString);

I also highly recommend that you change the name of your String to "intentString" instead of "intent." 我也强烈建议您将字符串的名称更改为“ intentString”,而不是“ intent”。 The name "intent" is typically used for actual Intent objects, not the String that you get out of the Intent . 名称“ intent”通常用于实际的Intent对象,而不是从Intent获取的String So naming it "intentString" makes things more readable for other developers. 因此,将其命名为“ intentString”会使其他开发人员更容易理解。

If you're trying to pass data back from SecondActivity to the MainActivity, then use startActivityForResult instead. 如果您试图将数据从SecondActivity传递回MainActivity,请改用startActivityForResult

Once you have launched your SecondActivity and ready to pass the data back to the MainActivity, create a new Intent and use SecondActivity.setResult(resultCode, Intent); 启动SecondActivity并准备将数据传递回MainActivity后,创建一个新的Intent并使用SecondActivity.setResult(resultCode, Intent); . Then call finish, to finish the SecondActivity. 然后调用finish,以完成SecondActivity。

Now, in your MainActivity, you will get a call to onActivityResult() which will give you the Intent you passed in the SecondActivity as a data parameter. 现在,在MainActivity中,您将调用onActivityResult() ,这将为您提供您在SecondActivity中作为数据参数传递的Intent。

You can look at this link for more info: http://developer.android.com/training/basics/intents/result.html 您可以查看此链接以获取更多信息: http : //developer.android.com/training/basics/intents/result.html

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

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