@@ -150,33 +150,40 @@ private void launchExistingNote(long accountId, long noteId) {
150150 launchExistingNote (accountId , noteId , null );
151151 }
152152
153+ private void launchExistingNote (long accountId , long noteId , @ Nullable final String mode ) {
154+ launchExistingNote (accountId , noteId , mode , false );
155+ }
153156
154157 /**
155158 * Starts a {@link NoteEditFragment} or {@link NotePreviewFragment} for an existing note.
156159 *
157- * @param noteId ID of the existing note.
158- * @param mode View-mode of the fragment (pref value or null). If null will be chosen based on
159- * user preferences.
160+ * @param noteId ID of the existing note.
161+ * @param mode View-mode of the fragment (pref value or null). If null will be chosen based on
162+ * user preferences.
163+ * @param discardState If true, the state of the fragment will be discarded and a new fragment will be created
160164 */
161- private void launchExistingNote (long accountId , long noteId , @ Nullable final String mode ) {
165+ private void launchExistingNote (long accountId , long noteId , @ Nullable final String mode , final boolean discardState ) {
162166 // save state of the fragment in order to resume with the same note and originalNote
163- Fragment .SavedState savedState = null ;
164- if (fragment != null ) {
165- savedState = getSupportFragmentManager ().saveFragmentInstanceState (fragment );
166- }
167- fragment = getNoteFragment (accountId , noteId , mode );
167+ runOnUiThread (() -> {
168+ Fragment .SavedState savedState = null ;
169+ if (fragment != null && !discardState ) {
170+ savedState = getSupportFragmentManager ().saveFragmentInstanceState (fragment );
171+ }
172+ fragment = getNoteFragment (accountId , noteId , mode );
168173
169- if (savedState != null ) {
170- fragment .setInitialSavedState (savedState );
171- }
172- getSupportFragmentManager ().beginTransaction ().replace (R .id .fragment_container_view , fragment ).commit ();
173- if (!fragment .shouldShowToolbar ()){
174- binding .toolbar .setVisibility (View .GONE );
175- }else {
176- binding .toolbar .setVisibility (View .VISIBLE );
177- }
174+ if (savedState != null ) {
175+ fragment .setInitialSavedState (savedState );
176+ }
177+ getSupportFragmentManager ().beginTransaction ().replace (R .id .fragment_container_view , fragment ).commit ();
178+ if (!fragment .shouldShowToolbar ()) {
179+ binding .toolbar .setVisibility (View .GONE );
180+ } else {
181+ binding .toolbar .setVisibility (View .VISIBLE );
182+ }
183+ });
178184 }
179185
186+
180187 private String getPreferenceMode () {
181188 final var prefKeyNoteMode = getString (R .string .pref_key_note_mode );
182189 final var prefKeyLastMode = getString (R .string .pref_key_last_note_mode );
@@ -289,10 +296,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
289296 close ();
290297 return true ;
291298 } else if (itemId == R .id .menu_preview ) {
292- changeMode (Mode .PREVIEW );
299+ changeMode (Mode .PREVIEW , false );
293300 return true ;
294301 } else if (itemId == R .id .menu_edit ) {
295- changeMode (Mode .EDIT );
302+ changeMode (Mode .EDIT , false );
296303 return true ;
297304 }
298305 return super .onOptionsItemSelected (item );
@@ -340,18 +347,18 @@ public void onNoteUpdated(Note note) {
340347 }
341348
342349 @ Override
343- public void changeMode (@ NonNull Mode mode ) {
350+ public void changeMode (@ NonNull Mode mode , boolean reloadNote ) {
344351 switch (mode ) {
345352 case EDIT :
346- launchExistingNote (getAccountId (), getNoteId (), getString (R .string .pref_value_mode_edit ));
353+ launchExistingNote (getAccountId (), getNoteId (), getString (R .string .pref_value_mode_edit ), reloadNote );
347354 break ;
348355 case PREVIEW :
349- launchExistingNote (getAccountId (), getNoteId (), getString (R .string .pref_value_mode_preview ));
356+ launchExistingNote (getAccountId (), getNoteId (), getString (R .string .pref_value_mode_preview ), reloadNote );
350357 break ;
351358 case DIRECT_EDIT :
352359 // TODO deal with conflict when doing some edits and then changing to direct edit
353360 // TODO deal with note not created yet on server
354- launchExistingNote (getAccountId (), getNoteId (), getString (R .string .pref_value_mode_direct_edit ));
361+ launchExistingNote (getAccountId (), getNoteId (), getString (R .string .pref_value_mode_direct_edit ), reloadNote );
355362 break ;
356363 default :
357364 throw new IllegalStateException ("Unknown mode: " + mode );
0 commit comments