Intent
类的直接putExtra()
函数将任意数量的值传递给新的Activity
。像这样:
Intent i = new Intent(this, MyActivity.class);
i.putExtra(ID_EXTRA1, "1");
i.putExtra(ID_EXTRA2, "111");
startActivity(i);
我知道在Android中有Bundle
,我也看到人们使用Bundle
来向新的Activity
传递值。
像这样:
Intent intent = new Intent(this, MyActivity.class);
Bundle extras = new Bundle();
extras.putString("EXTRA_USERNAME","my_username");
extras.putString("EXTRA_PASSWORD","my_password");
intent.putExtras(extras);
startActivity(intent);
我有两个疑问。
为什么我可以把值直接放到Intent
中,还要使用Bundle
呢?
相对于直接使用Intent
的putExtra()
方法,使用Bundle
有哪些优势?
startActivity(intent, options)
还是intent.putExtra()
中完成?我知道两者都可以,但我只是在考虑最佳实践?我通常不会触及传递给onCreate的Bundle,但我经验不足,所以正在寻求意见。 - WORMSS