Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

android - How to pass Bundle from Fragment to Fragment

I am using fragments in my application. This is my first fragment that simply inflate a xml file:

public class FragmentA extends SherlockFragment
{
    Context myContext,appContext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    myContext = getActivity();
    appContext=getActivity().getApplicationContext();
    arguments = getArguments();
    doctor_id=arguments.getInt("doctor_id");
    userType=arguments.getString("userType");
    return inflater.inflate(R.layout.left_panel, container,false);
}

and this is the left_panel .xml, that contains a fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <fragment
        android:id="@+id/titles"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        class="com.example.sample.ListFrag" />

</LinearLayout>

This is my ListFrag class:

public class ListFrag extends Fragment 
{
    Context myContext,appContext;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        layoutView = inflater.inflate(R.layout.activity_doctor_list,container);
        myContext = getActivity();
        appContext=getActivity().getApplicationContext();
        arguments=getArguments();
        int doctor_id=arguments.getInt("doctor_id");
}
}

I don't know how to pass Bundle arguments from FragmentA to ListFrag.

question from:https://stackoverflow.com/questions/17063378/how-to-pass-bundle-from-fragment-to-fragment

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your FragmentA fragment set the bundle as the argument.

Bundle args = new Bundle();
args.putInt("doctor_id",value);    
ListFrag newFragment = new ListFrag ();
newFragment.setArguments(args);

In your ListFrag fragment get the bundle as

Bundle b = getArguments();
int s = b.getInt("doctor_id");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...