本例,Activity从上一级activity中通过intent获取用户名和认证结果的参数,需要将参数传递给Fragment.
传递的方式是在Activity中通过bundle将数据打包,然后通过FragmentTransaction在Fragment创建是将bundle对象通过setArguments传给Fragment
代码示例如下:
Activity的代码:
package com.zoomactech.projectprogressmanager;
import com.zoomactech.projectprogressmanager.portalfragment.ProjectProgressSubFrag; //导入fragment的包
import android.os.Bundle;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.widget.TextView;
public class Ppm_main_portal extends FragmentActivity {
private String username = "";
private boolean ifauthened;
private TextView portal_welcome;
private FragmentManager fm;
private FragmentTransaction ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ppm_main_portal);
Intent reciIntent = getIntent(); //接收上一级activity传过来的参数
username = reciIntent.getStringExtra("username");
ifauthened = reciIntent.getBooleanExtra("ifauthened", false);
//System.out.println("++++++++++++++++++++" + username);
//System.out.println("********************" + ifauthened);
fm = this.getSupportFragmentManager();
ft = fm.beginTransaction();
ProjectProgressSubFrag PPMSubFragment = new ProjectProgressSubFrag(); //ProjectProgressSubFrag为fragment的类名
Bundle bundle = new Bundle(); //创建bundle来封装传递给fragment的参数
bundle.putString("username", username);
bundle.putBoolean("ifauthened", ifauthened);
PPMSubFragment.setArguments(bundle); //设置传递的对象
ft.add(R.id.frag_container_linearyout, PPMSubFragment, "projectprogresssubfrag"); //R.id.frag_container_linearyout为fragment的父容器
ft.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.ppm_main_portal, menu);
return true;
}
}
activity的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/frag_container_linearyout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#22333333"
android:gravity="top"
android:orientation="vertical" >
<!-- <fragment
android:id="@+id/frag_disp"
android:name="com.zoomactech.projectprogressmanager.portalfragment.ProjectProgressSubFrag"
android:layout_width="match_parent"
android:layout_height="match_parent" /> -->
</LinearLayout>
//............省略其他代码
</LinearLayout>
fragment的代码:
package com.zoomactech.projectprogressmanager.portalfragment;
import com.zoomactech.projectprogressmanager.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ProjectProgressSubFrag extends Fragment {
private TextView progress_sub_fragment_title;
private String title_str,username;
private boolean ifauthened;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.progress_sub_fragment, container, false);
Bundle bundle=this.getArguments();
username=bundle.getString("username");
ifauthened=bundle.getBoolean("ifauthened");
progress_sub_fragment_title=(TextView)view.findViewById(R.id.progress_sub_fragment_title);
title_str="这是个fragment,你好!传递的username为"+username+"传递的认证状态是"+ifauthened;
progress_sub_fragment_title.setText(title_str);
return view;
}
}
fragment的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/progress_sub_fragment_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="这是个Fragment!!!!!!!!!!!!!"
/>
</LinearLayout>
最后传递参数的结果如下图: