我创建了一个片段,其内容每天都在变化,问题是如果我不离开Fragment并重新打开它(这样我调用onCreateView方法)Fragment不会更新它的内容,以及我如何更新其内容?我看到如果我在后台发送应用程序并重新打开它,视图不会更新,这是一个问题......
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.scarsdale_home_activity, container, false);
d=new Database(getActivity());
String dataString=d.checkDataString();
int start = dataString.indexOf("/");
String dayString = dataString.substring(0,start);
String monthString = dataString.substring(start + 1,start+3);
String yearString = dataString.substring(start+4);
int day = Integer.parseInt(dayString);
int month = Integer.parseInt(monthString);
int year = Integer.parseInt(yearString);
Calendar current = Calendar.getInstance();
current.get(Calendar.YEAR);
current.get(Calendar.MONTH);
current.get(Calendar.DAY_OF_MONTH);
Calendar thatDay = Calendar.getInstance();
thatDay.set(Calendar.YEAR,year);
thatDay.set(Calendar.MONTH,month-1);
thatDay.set(Calendar.DAY_OF_MONTH,day);
long diff = current.getTimeInMillis() - thatDay.getTimeInMillis();
long days = diff / (24 * 60 * 60 * 1000);
String strDays = new Long(days).toString();
int intDays =Integer.parseInt(strDays);
TextView textsca1;
if(intDays >=0 && intDays<=6){
d= new Database(getActivity());
String listadietatext= d.getRow(intDays);
textsca1=(TextView) rootView.findViewById(R.id.textsca1);
textsca1.setText(listadietatext);
}
return rootView;
}
我尝试通过onResume进行更新但应用程序崩溃
@Override
public void onResume() {
super.onResume();
FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
fragTransaction.replace(R.id.container_sca,ScarsdaleHome.newIstance());
fragTransaction.commit();
}
答案 0 :(得分:2)
快速浏览片段生命周期指南here。碎片与其调用活动生命周期紧密耦合。在您的情况下,如果您覆盖片段中的OnResume()
方法,而不是onCreateView()
,那么当您的应用在后台进行并重新打开时,活动onResume
(以及片段{ {1}})都会被调用。