Android类参考---Fragment(六)

本文详细介绍了Fragment对象在Android应用中的生命周期方法及其属性解析过程,包括onInflate、onCreate、onCreateView等关键方法的实现与用途,以及如何通过属性和getArguments()方法获取参数,展示了Fragment在不同配置下灵活变化的实现方式。

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

public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState)

当一个Fragment对象被作为一个View对象布局的一部分来填充时,就会调用该方法,通常用于设置一个Activity的内容视窗。在从布局文件的标签中创建该Fragment对象之后,可以立即调用该对象。注意:这时的调用是在该Fragment对象的onAttach(Activity)方法被调用之前,因此在这时所能做的所有的事情就是解析并保存它的属性设置。

每次调用该方法时,该Fragment对象都被填充,即使是把它填充到一个新的用于保存状态的新的实例。通常每次都要重新解析参数,从而允许它们根据不同的配置来改变。

该方法在API Level 12中被引入。

以下是一个Fragment的典型实现,它能够同时通过属性和getArguments()方法来获取参数:

publicstaticclassMyFragmentextendsFragment{
CharSequence mLabel;

/**
* Create a new instance of MyFragment that will be initialized
* with the given arguments.
*/

staticMyFragment newInstance(CharSequence label){
MyFragment f =newMyFragment();
Bundle b =newBundle();
b
.putCharSequence("label", label);
f
.setArguments(b);
return f;
}

/**
* Parse attributes during inflation from a view hierarchy into the
* arguments we handle.
*/

@Overridepublicvoid onInflate(Activity activity,AttributeSet attrs,
Bundle savedInstanceState){
super.onInflate(activity, attrs, savedInstanceState);

TypedArray a = activity.obtainStyledAttributes(attrs,
R
.styleable.FragmentArguments);
mLabel
= a.getText(R.styleable.FragmentArguments_android_label);
a
.recycle();
}

/**
* During creation, if arguments have been supplied to the fragment
* then parse those out.
*/

@Overridepublicvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

Bundle args = getArguments();
if(args !=null){
mLabel
= args.getCharSequence("label", mLabel);
}
}

/**
* Create the view for this fragment, using the arguments given to it.
*/

@OverridepublicView onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState){
View v = inflater.inflate(R.layout.hello_world, container,false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText(mLabel !=null? mLabel :"(no label)");
tv
.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
return v;
}
}

要注使用意的是,使用styleable资源来解析XML属性。以下是styleable所使用的XML声明:

<declare-styleablename="FragmentArguments">
<attrname="android:label"/>
</declare-styleable>

然后,在Activity的内容布局内部,能够像下面这样声明一个Fragment标签:

<fragmentclass="com.example.android.apis.app.FragmentArguments$MyFragment"
android:id="@+id/embedded"
android:layout_width="0px"android:layout_height="wrap_content"
android:layout_weight="1"
android:label="@string/fragment_arguments_embedded"/>

Fragment对象也能够在运行时,通过在Bundle对象中参数来动态的创建,以下是一个动态创建Fragment对象的例子:

@Overrideprotectedvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView
(R.layout.fragment_arguments);

if(savedInstanceState ==null){
// First-time init; create fragment to embed in activity.
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment newFragment =MyFragment.newInstance("From Arguments");
ft
.add(R.id.created, newFragment);
ft
.commit();
}
}

参数

activity 指定要该Fragment对象来填充的Activity对象;

attrs 指定了正在创建的Fragment对象标签中的属性;

savedInstanceState 如果该Fragment对要重之前保存的状态中重建,那么就要使用该参数,它保存了该Fragment对象之前的状态。

public void onLowMemory()

当整个系统运行在低内存的状态,并当前活跃的运行进程视图回收内存的时候,会调用该方法。这个方法被调用的精确的时间点没有被定义,通常它会发生在所有的后台进程都被杀死的前后,这是在到达杀死进程托管服务的时点之前,并且会尽量避免杀死前台UI。

应用程序能够实现该方法,用于释放缓存或其他的不需要的资源。在从这个方法返回之后,系统会执行gc(垃圾回收)操作。

public boolean onOptionsItemSelected(MenuItem item)

选项菜单被选择的时候,系统会调用该方法。该方法的默认实现只是简单的返回false,默认实现只是执行一些正常的处理(如调用菜单项目的Runnable对象,或把一个消息发送给合适的Handler对象)。可以使用菜单项的这个方法,做那些没有其他措施处理的工作。

该类的任何子类都应该调用基类的实现,以便执行默认的菜单处理。

参数

item 用户选择的菜单项。

返回值

布尔值,返回false,则运行正常的菜单处理继续执行,否则会终止执行。

public void onOptionsMenuClosed(Menu menu)

在选项菜单被关闭的时候(既可以是用户按下了回退或菜单按钮取消了菜单,也可以是用户选择了一个菜单项),系统会调用这个方法。

参数

menu 最后显示的或由onCreateOptionsMenu()方法第一次初始化的选项菜单。

Public void onPause()

当该Fragment对象不再是恢复状态的时候,系统会调用该方法。这个方法通常会跟它的Activity的生命周期的Activity.onPause()方法捆绑。

public void onPrepareOptionsMenu(Menu menu)

该方法用于准备显示屏幕的标准选项菜单。它是选项菜单显示之前被调用的。能够使用这个方法来启用或禁用某些菜单项,或者动态的修改菜单项的内容。

参数

menu最后显示的或由onCreateOptionsMenu()方法第一次初始化的选项菜单。

Public void onResume()

当Fragment对象显示给用户并处于活跃的运行状态时,系统会调用这个方法。它通常会跟它的Activity生命周期的Activity.onResume()方法绑定。

public void onSaveInstanceState(Bundle outstate)

当要求Fragment对象保存当前的动态的状态时,系统会调用该方法,以便能够在以后的新实例重建时,使用这些被保存的状态。如果以后需要创建一个新的该Fragment对象实例,那么放置该方法Bundle参数中的数据,就会传递给onCreate(Bundle)、onCreateView(LayoutInflater, ViewGroup, Bundle)、onActivityCreated(Bundle)方法的Bundle参数)。

对应Activity.onSaveInstanceState(Bundle)方法的大多数讨论,也适用于本方法。但是要注意的是:这个方法可以在onDestroy()方法之前的任意时点调用。有一些情况是该Fragment对象已经被关闭了(如当它放置在没有UI显示的回退堆栈中时),但是直到它的Activity需要保存它的状态时,该Fragment的状态才会保存。

参数

outstate 该参数用于放置要保存的状态,它是一个Bundle类型的对象。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值