Fragment传递参数

来源:互联网 发布:阿里云域名认证步骤 编辑:程序博客网 时间:2024/05/01 01:15

转自:http://stackoverflow.com/questions/7149802/how-to-transfer-some-data-to-another-fragment

How to transfer some data to another Fragment?


up vote15down votefavorite
6

How to transfer some data to another Fragment likewise it was done with extras for intents?

share|improve this question
 add comment (requires an account with 50 reputation)

2 Answers

activeoldestvotes
up vote42down voteaccepted

Use a Bundle. Here's an example:

Fragment fragment = new Fragment();Bundle bundle = new Bundle();bundle.putInt(key, value);fragment.setArguments(bundle);

Bundle has put methods for lots of data types. Seehttp://developer.android.com/reference/android/os/Bundle.html

Then in your Fragment, retrieve the data (e.g. in onCreate()) with:

Bundle bundle = this.getArguments();int myInt = bundle.getInt(key, defaultValue);
share|improve this answer
 
getArguments is returning null – anirudhmaddy Apr 2 at 8:59
add comment (requires an account with 50 reputation)
up vote3down vote

getArguments() is returning null because "Its doesn't get anything"

Try this code to handle this situation

if(getArguments()!=null){int myInt = getArguments().getInt(key, defaultValue);}
share|improve this answer
 add comment (requires an account with 50 reputation)
原创粉丝点击