Fragment中调用getActivity为null的问题---------------->Activity与Fragment的调用关系

一,参考:
(1)Fragment中调用getActivity为null的问题
https://blog.csdn.net/goodlixueyong/article/details/48715661

二,定义
1.Fragment
A Fragment is a piece of an application’s user interface or behavior
that can be placed in an Activity. Interaction with fragments it done through
FragmentManager, which can be obtained via Activity#getFragmentManager()
and Fragment#getFragmentManager()

#OlderPlatforms
While the Fragment API was introduced in HONEYCOME(android API 11), a version of the API
at is also available for use on older platforms through android.support.v4.app.FragmentActivity.

#Lifecycle
Though a Fragment’s lifecycle is tied to its owning activity, it has its own wrinkle on the standard activity lifecycle. It includes basic activity lifecycle methods such as #onResume but also important are methods related to interactions whith the activity and UI generation.

The core series of lifecycle methods that are called to bring a fragment up to resumed state (interacing with the user) are:
{onAttach}: called once the fragment is associated with its activity.
{onCreate}: called to do initial creation of the fragment.
{onCreateView}: creates and return the view hierarchy associated with the fragment.
{onActivityCreated}:tells the fragment that its activity has completed its own Activity#onCreate
{onViewStateRestored}: tells the fragment that all of the saved state of its view hierarchy has been restored.
{onStart}:makes the fragment visible to the user(based on its containing activity being started)
{onResume}:makes the fragment begin interacting with the user(based on its containing activity being resumed).

As a fragment is no longer being used, it goes through a reverse series of callbacks:
{onPause}: fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
{onStop}: fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
{onDestroyView}: allows the fragment to clean up resources associated with its View.
{onDestroy}: called to do final cleanup of the fragment’s state.
{onDetach}: called immediately prior to the fragment no longer being associated with its activity

总结: 开始与onAttach,到Activity结束于onDetach;中间过程类似于Activity,但是onStart,onResume必须基于关联的Activity 也onStart, onResume; 然后onPause和onStop可自主但会受Activity的onPause和onStop影响也到达该状态.

#Layout

Fragments can be used as part of your application’s layout, allowing you to better modularize your code and more esily adjust your user interface to the screen it is running on.

sample:
https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java

总结:静态方面Fragment可以被内嵌到Activity的布局文件中使用;
动态方面可以通过下面两个方法和指定containerViewId的容器绑定.

/**

  • Add a fragment to the activity state. This fragmtn may optionally also have its view if {
  • Fragment#onCreateView returns non-null} inserted into a container view of the activity.
    */
    (1)FragmentManager.beginTransaction.add(@IdRes int containerViewId, Fragment fragment,
    String tag)

/**

  • Replace an existing fragment that was added to a container. This is essentially the same as *calling {#remove(Fragment)} for all currently added fragments that were added with the same containerViewId and then {#add(int, Fragment, String)} with the same arguments given here
    */
    (2)FragmentManager.beginTransaction.(@IdRes int containerViewId, Fragment fragment,
    String tag)

#BackStack

The transaction in which fragments are modified can be placed on an internal back-stack of the
owning activity. when the user presses back in the activity, any transactions on the back stack are
popped off before the activity itself is finished.

sample: https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java

总结:Activity内部有一个栈用来管理和它关联的Fragment.

2.Activity
An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI
with {#setContentView}. While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with {android.R.attr#windowIsFloating} set), Muti-Window mode or embedded into other windows.

三,问题
0,Activity和Fragment的关系
A Fragment is closely

  • tied to the Activity it is in, and can not be used apart from one. Though
  • Fragment defines its own lifecycle, that lifecycle is dependent on its
  • activity: if the activity is stopped, no fragments inside of it can be
  • started; when the activity is destroyed, all fragments will be destroyed.

1.Activity如何创建?

2.Fragment如何创建?

3.Fragment如何与Activity关联?

4.Activity的savedInstanceState如何产生,存了什么,如何使用?

5.FragmentManager的作用?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值