我创建了一个片段,其内容每天都在变化,问题是如果我不离开片段并重新打开它(以这种方式,我称为onCreateView方法),该片段就不会更新其内容,因此我该如何更新其内容?我看到如果我在后台发送该应用程序并重新打开它,则视图不会更新,这是一个问题...

 @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();

  }

最佳答案

快速浏览Fragment Lifecycle指南here。片段与其呼叫活动生命周期紧密相关。在您的情况下,如果您在片段中替代OnResume()方法而不是onCreateView(),那么当您的应用进入后台并重新打开时,活动onResume(和片段onResume)都将是叫。

关于android - 即使未调用onCreateView,也刷新当前 fragment ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29692557/

10-08 23:46