如何从Activity传递参数到Fragment中

本文演示了如何在Android中从Activity通过Intent获取数据,并使用Bundle和FragmentTransaction将参数传递给Fragment。详细步骤包括创建Bundle,设置Fragment的Arguments,以及在Fragment中解析接收到的参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本例,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>

 

最后传递参数的结果如下图:

 

 


 

 

 


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值