Android Fragment getActivity返回null解决

Android Fragment getActivity返回null解决
1.
在Android开发中,如果我们用到V4包里面的Fragment,在应用被切换到后台的时候,Activity可能被回收,但是创建的所有Fragment则会被保存到Bundle里面,下面是FragmentActivity的部分源码/**
* Save all appropriate fragment state.
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Parcelable p = mFragments.saveAllState();
if (p != null) {
outState.putParcelable(FRAGMENTS_TAG, p);
}
}
,如果从最近使用的应用里面点击我们的应用,系统会恢复之前被回收的Activity,这个时候FragmentActivity在oncreate里面也会做Fragment的恢复,@Override
protected void onCreate(Bundle savedInstanceState) {
mFragments.attachActivity(this, mContainer, null);
// Old versions of the platform didn’t do this!
if (getLayoutInflater().getFactory() == null) {
getLayoutInflater().setFactory(this);
}

    super.onCreate(savedInstanceState);
    
    NonConfigurationInstances nc = (NonConfigurationInstances)
            getLastNonConfigurationInstance();
    if (nc != null) {
        mAllLoaderManagers = nc.loaders;
    }
    if (savedInstanceState != null) {
        Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
        mFragments.restoreAllState(p, nc != null ? nc.fragments : null);
    }
    mFragments.dispatchCreate();
}

但是此时恢复出的Fragment,在调用getActivity的时候会返回null,具体什么原因还没详细研究,这里我的解决方法是在恢复 Fragment之前把保存Bundle里面的数据给清除,也就是保存的Fragment信息,然后自己重新replace。方法如下:
if(arg0 != null)
{
String FRAGMENTS_TAG = “android:support:fragments”;
// remove掉保存的Fragment
arg0.remove(FRAGMENTS_TAG);
}
2.
在使用fragment时候遇到了一个问题,如果这个应用是被系统回收的,再回到这个应用时,fragment调用getActivity返回null,该fragement无法显示
public class MainActivity extends ActionBarActivity
implements ActionBar.OnNavigationListener{

private PlaceholderFragment subjectList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

subjectList = (PlaceholderFragment)PlaceholderFragment.instantiate(this,PlaceholderFragment.class.getName());
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, subjectList)
.commit();
}
}
}
public class PlaceholderFragment extends ListFragment
implements LoaderManager.LoaderCallbacks{

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);

return rootView;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (getActivity() == null) {
Log.v(“PlaceholderFragment onActivityResult”,“Not Attached”);
return;
}
}
}
错误代码如上,手动运行正常,退出再启动也是正常的。在系统回收资源再进入后出现问题Fragment 中getActivity() 为null。
刚开始以为是Activity和Fragment的lifecycle配合问题,可能某处的lifecycle处理不对,尝试在各个onXXX中增加处理,都不能解决问题。
后来想到有可能能是系统回收的时候仅回收了Activity,或者仅仅是将Activity和Fragment保存在某处,并未正真的释放。通过FragmentManager尝试找回先前的Fragment,问题解决。
在MainActivity中增加如下代码问题解决:
@Override
protected void onCreate(Bundle savedInstanceState) {

if (savedInstanceState == null)
subjectList = (PlaceholderFragment)PlaceholderFragment.instantiate(this,PlaceholderFragment.class.getName());
else
subjectList = (PlaceholderFragment)getSupportFragmentManager().getFragment(savedInstanceState,PlaceholderFragment.class.getName());

}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState,PlaceholderFragment.class.getName(),subjectList);
}

你可能感兴趣的:(安卓基础)