4 回答
TA贡献1891条经验 获得超3个赞
要使用 Intent 将数据从一个活动传递到其他活动,请确保您执行了以下步骤。
步骤 1 在源活动中创建一个新的 Explicit 或 Implicit Intent 对象。
步骤 2 调用intent.putExtra(String key, Object data)方法在其中保存数据。
步骤 3 在源活动中调用 startActivity(intent) 方法将意图传递给 android os。
步骤 4 在目标活动中调用 getIntent() 方法。
在你的情况下,我认为你错过了第 2 步
TA贡献1844条经验 获得超8个赞
是否可以使用 abundle来给出活动之间的信息
Bundle bundle = new Bundle();
bundle.putInt("value_name", 0);// 0 = value
bundle.putString("value_name", "text"); // Text = value string
Intent intent = new Intent(this, SecondActivty.class);
intent.putExtras(bundle);
startActivity(intent);
并抓住你的第二个活动
public class SecondActivty extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activty);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
int value = savedInstanceState.getInt("value_name");
}
TA贡献1825条经验 获得超4个赞
在您当前的 Activity 中,创建一个新的 Intent:
String value="Hello world";
Intent i = new Intent(CurrentActivity.this, NewActivity.class);
i.putExtra("key",value);
startActivity(i);
然后在新的 Activity 中,检索这些值:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
//The key argument here must match that used in the other activity
}
使用这种技术,您将能够在活动中传递变量
TA贡献1812条经验 获得超5个赞
您正在使用 getApplicationContext 但也许您正在尝试使用Activity 中的getApplication方法。我不建议使用这种方法将对象从一个活动传递到另一个活动。您应该使用 Intent 对象。看看官方文档
添加回答
举报
