安卓开发:Fragment使用

来源:互联网 发布:com和cn域名哪个好 编辑:程序博客网 时间:2024/05/16 16:12

Fragment作为碎片,依托于Activity,类似于Activity,在开发中经常用到。
由于Fragment依赖于Activity。所以Activity的生命周期直接影响附着于该Activity上的Fragment的生命周期。

Activity函数动作与Fragment之间的关系

Activity【动作】 Fragment【回调】created         onAttach()                onCreate()                onCreateView()                onActivityCreated()-----------------------------------started         onStart()-----------------------------------resumed         onResumed()-----------------------------------paused          onPause()-----------------------------------stopped         onStop()-----------------------------------destroyed       onDestroyView()                onDestroy()                onDetach()

onAttach():
与某Activity关联的时候调用

onCreate()和onCreateView():
Fragment中的onCreate()函数用于为当前Fragment初始化除View【布局文件】以外的属性,而onCreateView()函数用于为当前Fragment创建相应的视图,并且onCreate()需要返回View给调用者。所以也可以很好的理解onDestroyView()函数和onDestroy()函数之间的区别了。

onActivityCreated():
当Activity调用了onCreate()函数后,会回调该方法。

onDetach():
当Activity与当前Fragment解除关联的时候调用。

在Activity中显示Fragment:
Fragment.xml

<?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:layout_width="match_parent"        android:layout_height="match_parent"        android:text="fragment" /></LinearLayout>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <fragment        android:id="@+id/fragment"        android:name="com.example.aiden.myapplication.MyFragment"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>

MyFragment.java:

package com.example.aiden.myapplication;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class MyFragment extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment, container, false);    }}

MainActivity.java:

package com.example.aiden.myapplication;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity {    @Override    public void onCreate(Bundle save) {        super.onCreate(save);        this.setContentView(R.layout.activity_main);    }}

代码是平淡的,但是却出现了很多问题
如果在main_Activity.xml上增加一个按钮,为该按钮增加点击监听事件,点击按钮后,调用this.getFragmentManager().beginTransaction().remove(new MyFragment()).commit()是不会移除上面的MyFragment的。我们可以这么理解,由于是静态的,我们无法获取该Activity上的实例对象,所以无法移除。如果要移除,只能动态的增加MyFragment实例到Activity上,并且移除同一MyFragment实例,最后要调用commit()方法!

FragmentActivity和Fragment的区别:
FragmentActivity主要是解决Api3.0之前没有出现Fragment类,但是要使用Fragment这个类出现的尴尬问题。我们只要把Activity继承自FragmentActivity即可,获取Fragment从getFragmentManager()变成了getSupportFragmentManager()

0 0