
当我按下后退按钮时,我想刷新标签栏活动中的一个片段.我怎样才能实现这一目标?我尝试了什么
java.lang.NullPointerException: Attempt to invoke virtual method
‘androID.support.v4.app.FragmentTransaction
androID.support.v4.app.FragmentManager.beginTransaction()’ on a null
object reference
片段简介
public voID refreshFragment(){ FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.detach(this).attach(this).commit();}编辑个人资料活动
@OverrIDepublic voID onBackpressed() { super.onBackpressed(); FragmentProfile fragment = new FragmentProfile(); fragment.refreshFragment();}解决方法:
您可以使用SharedPreferences执行此 *** 作.
第1步:创建一个常量类.
public class Constants { public static final String REFRESH = "refresh_content";}第2步:将boolean放到编辑配置文件活动上的constants.REFRESH中,
private SharedPreferences pref;@OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { pref = PreferenceManager.getDefaultSharedPreferences(getContext()); return inflater.inflate(R.layout.fragment_rostering, container, false);}public voID onBackpressed() { SharedPreferences.Editor editor = pref.edit(); editor.putBoolean(Constants.REFRESH, true); editor.commit(); super.onBackpressed();}private SharedPreferences pref;@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { pref = PreferenceManager.getDefaultSharedPreferences(getContext()); return inflater.inflate(R.layout.fragment_profile, container, false);}@OverrIDepublic voID onResume() { super.onResume(); if(pref.getBoolean(Constants.REFRESH, false)){ SharedPreferences.Editor editor = pref.edit(); editor.putBoolean(Constants.REFRESH, false); editor.commit(); refreshFragment(); }} 总结 以上是内存溢出为你收集整理的java – onBackPressed和调用刷新功能android全部内容,希望文章能够帮你解决java – onBackPressed和调用刷新功能android所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)