9 Fragment与Activity通信

9-1 Fragment的生命周期(一)

1、 onAttach()://当fragment被添加到activity中时会回调这个方法;只会调用一次
2、 onCreate();//在fragment创建的时候回调这个方法;只会调用一次;
3、 onCreateView()://每次创建都会绘制Fragment的View组件时调用该方法,相当于fragment绑定一个布局,布局文件转换成view返回回来;
4、 onActivityCreated();//fragment所在的activity启动完成后调用;
5、 onStart():启动Fragment时会被回调,
6、 onResume()方法,调用onStart()方法后面一定会调用这个方法,恢复fragment时回调
7、 onPause():暂停Fragment
8、 onStop();//停止Fragment
9、 onDestroyView():销毁Fragment所包含的View组件,与onCreateView相对应
10、 onDestroy():销毁Fragment时会被回调
11、 onDetach():Fragment从Activity中删除时回调这个方法,并且只调用一次

启动fragment
onAttach()->onCreate()->onCreateView()->onActivityCreated()->onStart()->onResume()

屏幕锁屏
onPause()->onStop()

屏幕解锁
onStart()->onResume()

第一个fragment切换到第二个fragment,切换到其他Fragment
第一个onPause()->onStop()->onDestoryView()->onDestory()->onDetach();
第二个onAttach()->onCreate()->onCreateView()->onActivityCreated()->onStart()->onResume()

Ps:类似Activity周期,只是更详细,但是OnStop()后,重启时没ReStart();直接OnStart()——OnResume()。

package com.example.android_fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment3 extends Fragment {

    private TextView tv;

    // 启动Fragment——>屏幕锁屏——>屏幕解锁——>
    //切换到其他的Fragment——>回到桌面——>回到应用——>退出Fragment
    /**
     * 每次创建都会绘制Fragment的View组件时回调该方法
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment2, container, false);
        TextView tv = (TextView) view.findViewById(R.id.text);
        tv.setText("第一个Fragment");
        Log.i("Main", "Fragment1---onCreateView()");
        return view;
    }

    /**
     * 当Fragment被添加到Activity时候会回调这个方法,并且只调用一次
     */
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        Log.i("Main", "Fragment1---onAttach()");

    }

    /**
     * 创建Fragment时会回调,只会调用一次
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Log.i("Main", "Fragment1---onCreate()");

    }

    /**
     * 当Fragment所在的Activty启动完成后调用
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        Log.i("Main", "Fragment1---onActivityCreated()");

    }

    /**
     * 启动Fragment
     * 
     */
    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.i("Main", "Fragment1---onStart()");

    }

    /**
     * 恢复Fragment时会被回调,调用onStart()方法后面一定会调用onResume()方法
     */
    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.i("Main", "Fragment1---onResume()");

    }

    /**
     * 暂停Fragment
     */
    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.i("Main", "Fragment1---onPause()");

    }

    /**
     * 停止Fragment
     */
    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.i("Main", "Fragment1---onStop()");

    }

    /**
     * 销毁Fragment所包含的View组件时
     */
    @Override
    public void onDestroyView() {
        // TODO Auto-generated method stub
        super.onDestroyView();
        Log.i("Main", "Fragment1---onDestroyView()");

    }

    /**
     * 销毁Fragment时会被回调
     */
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.i("Main", "Fragment1---onDestroy()");

    }

    /**
     * Fragment从Activity中删除时会回调该方法,并且这个方法只会调用一次
     */
    @Override
    public void onDetach() {
        // TODO Auto-generated method stub
        super.onDetach();
        Log.i("Main", "Fragment1---onDetach()");
    }
}

 

9-2 Fragment的生命周期(二)

MainActivity.java

package com.example.android2_fragment;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.view.Menu;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity implements OnCheckedChangeListener
{
    private RadioGroup group;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        group = (RadioGroup) findViewById(R.id.radiogroup);
        group.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        // TODO Auto-generated method stub
        switch (checkedId)
        {
        case R.id.first:
        {
            Intent intent = new Intent(this, MainActivity2.class);
            startActivity(intent);
            break;
        }
        case R.id.second:
        {
            MyFragment2 myFragment2 =new MyFragment2();//new出一个fragment对象
            FragmentManager fragmentManager=getFragmentManager();//得到fragment管理者
            FragmentTransaction beginTransaction=fragmentManager.beginTransaction();//开始一个事物
            beginTransaction.add(R.id.frame, myFragment2);//fragment事物添加一个fragment,
            //R.id.fragment为fragment显示的位置id,fragment2为需要添加的fragment;
            beginTransaction.addToBackStack(null);
            //增加回退效果,手机物理返回按键可以回退到上一个界面而不是直接退出
            beginTransaction.commit();//最后提交
            break;
        }
        case R.id.thrid:
        {
            Intent intent = new Intent(MainActivity.this,MyFragment3.class);
            startActivity(intent);
            break;
        }
        case R.id.fourth:
        {
            break;
        }
        }
    }
}

MainActivity3.java

package com.example.android2_fragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity3 extends Activity {

    private Button button;
    private Fragment frag;
    private boolean flag = true;

    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main3);
        init();
        button = (Button) findViewById(R.id.change_button);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
                
                if (flag) {
                    MyFragment4 frag4=new MyFragment4();
                    beginTransaction.replace(R.id.layout4, frag4);
                    flag=false;

                } else {
                    MyFragment3 frag3=new MyFragment3();
                    beginTransaction.replace(R.id.layout3, frag3);
                    flag=true;

                }
                
               beginTransaction.commit();
            }
        });

    }
    private void init() {
        // TODO Auto-generated method stub
        //得到fragment管理者
        FragmentManager fragmentManager = getFragmentManager();
        //开始一个事物
        FragmentTransaction beginTransaction = fragmentManager
                .beginTransaction();
        //new出一个fragment对象
        MyFragment3 frag3 = new MyFragment3();
        //fragment事物添加一个fragment,
        beginTransaction.add(R.id.layout3,frag3);
        beginTransaction.commit();
    }

}

main3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:text="切换Fragment"
        android:id="@+id/change_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </Button>

</LinearLayout>

    

 

9-3 Activity向Fragment传递数据

Fragment与Activity通信

  (1)Fragment可调用getActivity()方法去获取它所在的Activity
  (2)Activity可调用FragmentManager的findFragmentById()或findFragmentByTag()方法获取Fragment

  Activity->Fragment:在Activity中创建一个Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法

  Fragment->Activity:需要在Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回调接口。这样Fragment可调用该回调方法将数据传递给Activity.

 

MainActivity.java

package com.example.android2_fragment;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.view.Menu;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity implements OnCheckedChangeListener
{
    private RadioGroup group;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        group = (RadioGroup) findViewById(R.id.radiogroup);
        group.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        // TODO Auto-generated method stub
        switch (checkedId)
        {
        case R.id.first:
        {
            Intent intent = new Intent(this, MainActivity2.class);
            startActivity(intent);
            break;
        }
        case R.id.second:
        {
            MyFragment2 myFragment2 =new MyFragment2();//new出一个fragment对象
            FragmentManager fragmentManager=getFragmentManager();//得到fragment管理者
            FragmentTransaction beginTransaction=fragmentManager.beginTransaction();//开始一个事物
            beginTransaction.add(R.id.frame, myFragment2);//fragment事物添加一个fragment,
            //R.id.fragment为fragment显示的位置id,fragment2为需要添加的fragment;
            beginTransaction.addToBackStack(null);
            //增加回退效果,手机物理返回按键可以回退到上一个界面而不是直接退出
            beginTransaction.commit();//最后提交
            break;
        }
        case R.id.thrid:
        {
            Intent intent = new Intent(MainActivity.this,MainActivity3.class);
            startActivity(intent);
            break;
        }
        case R.id.fourth:
        {
            Intent intent = new Intent(MainActivity.this,MainActivity4.class);
            startActivity(intent);
            break;
        }
        }
    }

}

MainActivity4.java

package com.example.android2_fragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity4 extends Activity
{
    private EditText editText;
    private Button send;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main4);
        editText = (EditText) findViewById(R.id.editText);
        send = (Button) findViewById(R.id.send);
        send.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                String text = editText.getText().toString();
                MyFragment5 fragment5 = new MyFragment5();
                Bundle bundle = new Bundle();
                bundle.putString("name", text); //"name"为对应的key,text是传递的
         string fragment5.setArguments(bundle); 
//args传入Bundle包,发送数据给myFragment5
         /*在Activity中我们通过调用setArgument()方法给Fragment传值,
            在Fragment中,我们通过getArgument()方法获取从Activity中传过来的值。
            返回的对象是一个Bundle,再通过调用Bundle的get()方法,通过key就可以获得value值了。*/
FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); fragmentTransaction.add(R.id.layout4, fragment5, "fragment5");;//动态生成fragment的tag fragmentTransaction.commit();//记得提交事务
         Toast.makeText(MainActivity4.this, "向fragment发送数据" + text, Toast.LENGTH_LONG).show(); 
        }
    });
  }
}

main4.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送" />

</LinearLayout>

MyFragment5.java

package com.example.android2_fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MyFragment5 extends Fragment
{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment2, container, false);
        TextView tv = (TextView) view.findViewById(R.id.text);
        String string = getArguments().get("name") + "";//把“name”key对应的value拿出来
  [接收数据那里getArguments()后面为什么要加 + “” 这个   getArguments()返回的是一个Bundle对象然用Bundle对象的.get(key)按照key取得value值。   value值是一个Object类型的   最后赋值给String类型的了,所以要转换成String类型的   +“”和toString()方法一样的,都是转换成String类型的] tv.setText(string); Toast.makeText(getActivity(), "已成功接收到" + string, Toast.LENGTH_LONG) .show(); return view; } }

    

    

-------------------------------------------------------------------------------

步骤:

第一步 先定义main4.xml布局文件
第二步 MainActivity4中加载main4.xml (setContentView(R.layout.main4))
第三步 建立新的MyFragment5类去接收Activity4传递过来的信息,并加载所对应的布局文件
第四步 在android_fragmen中声明MainActivity4
第五步 按钮加跳转
第六步 MainActivity4的onClick中初始化MyFragment5
第七步 获取Fragment管理者并开启事物
第八步 把Fragment5添加进来(beginTransaction)
九 在MyFragment5 获取数据包 getArguments

1.Fragment可调用getActivity()方法获取对应的Activity;
2.Activity可调用FragmentManager的findFragmentById()或者findFragmentByTag()方法获取Fragment;
Fragment调用Activity里的数据
1.在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法
Activity调用Fragment里的数据
1.需要在Fragment中定义一个内部回调接口,让包含该Fragment的Activity实现该回调接口,这样Fragment可调用该回调方法将数据传递给Activity。

 

9-4 Fragment向Activity传递数据

动态加载的方式:

MyFragment5.java 

package com.example.android2_fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MyFragment5 extends Fragment
{
    private String codeString="thank you,Activity!";
    
    public MyListener listener;
     public interface MyListener{
         public void thank(String codeString);
     }
     
     @Override
    public void onAttach(Activity activity)
    {
        // TODO Auto-generated method stub
         listener=(MyListener) activity;
        super.onAttach(activity);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment2, container, false);
        TextView tv = (TextView) view.findViewById(R.id.text);
        String string = getArguments().get("name") + "";//接收activity传递过来的数据
        
        tv.setText(string);
        Toast.makeText(getActivity(), "已成功接收到" + string, Toast.LENGTH_LONG)
                .show();
        Toast.makeText(getActivity(), "向Activity发送" + codeString, Toast.LENGTH_LONG)
        .show();
        listener.thank(codeString);
        return view;
    }
}

MainActivity4.java

package com.example.android2_fragment;

import com.example.android2_fragment.MyFragment5.MyListener;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity4 extends Activity implements MyListener
{
    private EditText editText;
    private Button send;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main4);
        editText = (EditText) findViewById(R.id.editText);
        send = (Button) findViewById(R.id.send);
        send.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                String text = editText.getText().toString();// 获取编辑框输入的内容
                MyFragment5 fragment5 = new MyFragment5();// new一个fragment对象
                Bundle bundle = new Bundle();
                bundle.putString("name", text);
                fragment5.setArguments(bundle);// bundle传入数据
                /**
                 * 下面是动态启动fragment
                 * */
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                fragmentTransaction.add(R.id.layout4, fragment5, "fragment5");
                fragmentTransaction.commit();
                Toast.makeText(MainActivity4.this, "向fragment发送数据" + text,
                        Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void thank(String codeString)
    {
        // TODO Auto-generated method stub
        Toast.makeText(this, "已成功接收到" + codeString + ",客气啦!",
                Toast.LENGTH_SHORT).show();
    }
}

    

 

静态加载:

MainActivity4.java

package com.example.android2_fragment;

import com.example.android2_fragment.MyFragment5.MyListener;
import com.example.android2_fragment.MyFragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity4 extends Activity implements MyListener
{
    private EditText editText;
    private Button send;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main4);
        editText = (EditText) findViewById(R.id.editText);
        send = (Button) findViewById(R.id.send);
        send.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                String text = editText.getText().toString();// 获取编辑框输入的内容
                MyFragment5 fragment5 = new MyFragment5();// new一个fragment对象
                Bundle bundle = new Bundle();
                bundle.putString("name", text);
                fragment5.setArguments(bundle);// bundle传入数据
                /**
                 * 下面是动态启动fragment
                 * */
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                fragmentTransaction.add(R.id.layout4, fragment5, "fragment5");
                fragmentTransaction.commit();
                Toast.makeText(MainActivity4.this, "向fragment发送数据" + text,
                        Toast.LENGTH_LONG).show();
            }
        });
        FragmentManager fragmentManager=getFragmentManager();
        Fragment findFragmentById=fragmentManager.findFragmentById(R.id.frag);
        MyFragment frag=(MyFragment)findFragmentById;
        frag.setAaString("fragment的静态传值");
    }

    @Override
    public void thank(String codeString)
    {
        // TODO Auto-generated method stub
        Toast.makeText(this, "已成功接收到" + codeString + ",客气啦!",
                Toast.LENGTH_SHORT).show();
    }
}

main4.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送" />
    
    <fragment 
        android:id="@+id/frag"
        android:name="com.example.android2_fragment.MyFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

MyFragment.java

package com.example.android2_fragment;

import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyFragment extends Fragment
{
    private String aaString;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        // layout布局文件转化为View对象
        /*
         * inflate(resource, root, attachToRoot); 
         * resource:Fragment需要加载的布局文件
         * root:加载layout的父ViewGroup 
         * attactToRoot:false,不返回父ViewGroup
         */
        View view=inflater.inflate(R.layout.fragment, container, false);
        Button button=(Button)view.findViewById(R.id.button);
        TextView textView=(TextView)view.findViewById(R.id.GB_text);
        textView.setText("静态加载Fragment");
        button.setText("获取内容");
        button.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View arg0)
            {
                // TODO Auto-generated method stub
                String string=getAaString();
                Toast.makeText(getActivity(), string, Toast.LENGTH_SHORT);
            }
        });
        return view;
    }

    public String getAaString()
    {
        return aaString;
    }

    public void setAaString(String aaString)
    {
        this.aaString = aaString;
    }
}

Fragment与Activity的通信需要注意的3点:
1.Activity传值给Fragment通过Bundle
2.Fragment传值给Activity通过在Fragment中定义接口,实现Attach(Activity activity),在Activity中实现该接口
3.除以上方法之外,还能通过静态传值,静态传值在Fragment中定义Get,Set方法。

第一步 在MyFragment5写一个内部的接口——MyListener监听器
第二步 在MainActivity4实现好MyListener接口
第三步 在MyFragment5引入onAttach方法 并写一个MyListener对象listener
第四步 把activity传递给listener
第五步 在MainActivity4写一个感谢Toast.makeText 回到MyFragment5再加一条MyFragment5

第一步 在main4声明一个FrameLayout标签 里面包含一个android name ,id,宽高等
第二步 在MainActivity4中获取管理者 并且通过find byid调用
第三步 在MyFragment定义aaa 并设置传递和获取的方法 再通过find byid调用
第四步 在MyFragment改变button的逻辑(findById) 然后加OnClickListener
第五步 在OnClick传递aaa并且打印

静态加载Fragment:
Intent到一个新的Activity2上,Activity2的XML文件上添加一个Fragment控件,并在name属性上关联fragment对应的MyFragment类,在MyFragment类中添加控件。
打开Activity2就能打开对应的MyFragment并能通过findViewById找到在MyFragment上的空间。
动态加载Fragment:
创建一个FragmentManager,然后创建FragmentManager的beginTranscation。
然后使用beginTranscation的add()方法给Activity动态添加Fragment的类对象、replace()方法更改Fragment的类对象。
最后使用beginTranscation的commit()方法确认。

 

posted @ 2016-03-27 10:41  沉默的羊癫疯  阅读(140)  评论(0编辑  收藏  举报