2016-10-01 77 views
1

Fragment保留状态令我感到不舒服。我不知道我犯了什么错误。请帮助我们。片段定位管理

public class FragmentNotification extends Fragment implements OnAruguments { 

    public static final String LIST_ITEMS = "list_items"; 
    public static final String TEMP_ITEMS = "temp_items"; 

    List<DBNotifications> notificationModels = new ArrayList<>(); 
    List<DBNotifications> tempModelList = new ArrayList<>(); 
    NotificationAdapter notificationAdapter; 

    void setAdapter() { 
     if (notificationAdapter == null) { 
      notificationAdapter = new NotificationAdapter(getActivity(),  tempModelList); 
      listView.setAdapter(notificationAdapter); 
     } else { 
      notificationAdapter.notifyDataSetChanged(); 
     } 
     if (tempModelList.size() > 0) { 
      listView.setVisibility(View.VISIBLE); 
      cstmTxtNoItems.setVisibility(View.GONE); 
     } else { 
      listView.setVisibility(View.GONE); 
      cstmTxtNoItems.setVisibility(View.VISIBLE); 
     } 
    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     if (savedInstanceState != null) { 
      notificationModels = savedInstanceState.getParcelableArrayList(LIST_ITEMS); 
      tempModelList = savedInstanceState.getParcelableArrayList(TEMP_ITEMS); 
     } 
    } 

    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setRetainInstance(true); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     view = inflater.inflate(R.layout.notification, container, false); 
     init(); 
     createItemsList(); 

     return view; 
    } 

    void init() { 
     listview= (ListView) view.findViewById(R.id.listView); 
    } 

    void createItemsList() { 
     new NotificationModelCreation().execute(); 
    } 

    void createNotificationModelList() { 
     String query = "SELECT * FROM notifications WHERE id='" + tempModel.getId() + "' AND code='" + Constants.CODE + "' ORDER BY priority DESC "; 
     List<Notifications> notificationsList = Notifications.findWithQuery(Notifications.class, query); 
     if (notificationsList != null && notificationsList .size() > 0) { 
      notificationModels.addAll(notificationsList); 
      tempModelList.addAll(notificationsList); 
     } 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 

     setAdapter();// I have tried to set adapter onResume. Because, onCreateview will not be called on setRetainInstance =true 
    } 

    @Override 
    public void setOnArguments(Bundle bundle) {} 

    class NotificationModelCreation extends AsyncTask<Void, Void, Void> { 
     @Override 
     protected Void doInBackground(Void... params) { 
      createNotificationModelList(); 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void aVoid) { 
      super.onPostExecute(aVoid); 
      setAdapter(); 
     } 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putParcelableArrayList(LIST_ITEMS, (ArrayList<? extends Parcelable>) notificationModels); 
     outState.putParcelableArrayList(TEMP_ITEMS, (ArrayList<? extends Parcelable>) tempModelList); 

    } 
} 

当它第一次加载时,它运行良好。但是当配置改变时,设置了Adapter,但没有显示视图。 ListView也已初始化。因为,我通过这样的标签获得Fragment

getSupportManager().findFragmentByTag("tag"); 

在得到Fragment之后,我用下面的代码。

FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
ft.setCustomAnimations(R.anim.enter_from_right, 
     R.anim.exit_to_left, 
     R.anim.enter_from_right, 
     R.anim.exit_to_left) 
     .add(R.id.fragment_container, fragment, tag) 
     .addToBackStack(fragment.getClass().getName()) 
     .commit(); 

我也管理了backstack。那里没有问题。唯一的问题是视图不可见。即使列表中包含项目,并且ListView也会与适配器一起完成初始化。

被修改:

我除去notifiyDataSetChanged()和i设置适配器每次。现在它工作正常。

回答

0

将这个行活动中加载这个片段

android:configChanges="keyboardHidden|orientation" 

清单标签也不会影响任何数据。但是方向改变

快乐作弄!