2016-10-01 46 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" 

清單標籤也不會影響任何數據。但是方向改變

快樂作弄!