修改一个存在的 DialogFragment

Modify an exist DialogFragment

我想知道是否有任何方法可以像getFragmentByTag一样获取DialogFragment?

我会解释的。我正在使用对话框片段从用户那里获取一些信息,然后我将信息设置为可扩展列表视图的子数据。

然后我创建了一个允许用户编辑子数据的按钮。我想重新打开同一个 DialogFragment 并在 EditText 字段中设置旧数据。 这是我的代码:

NewExerciseChildFragment editExercise = new NewExerciseChildFragment();
Bundle bundle = new Bundle();
bundle.putInt("groupPosition", groupPosition);
bundle.putString("exerciseName", exerciseName);
bundle.putDouble("weight", weight);
bundle.putInt("reps", reps);
bundle.putInt("sets", sets);

editExercise.setArguments(bundle);
editExercise.show(getFragmentManager(), "showEditedExercise"); 

问题是,我创建了一个新的 DialogFragment,这意味着我不会编辑数据而是添加另一个子项。

有什么想法吗?

根据文档,您有两种显示 DialogFragment 的选项,作为 Dialog ("pop-up") 或 Embedding

Selecting Between Dialog or Embedding

A DialogFragment can still optionally be used as a normal fragment, if desired. This is useful if you have a fragment that in some cases should be shown as a dialog and others embedded in a larger UI. This behavior will normally be automatically selected for you based on how you are using the fragment, but can be customized with setShowsDialog(boolean).

ref: https://developer.android.com/reference/android/app/DialogFragment

Dialogs 旨在用作 "pop up"*,通常在解雇后销毁。此选项将需要传递对驻留在 ExapandableListView 中的 data/model/object 的引用(或它的某个子项,如您所述)。该对象需要公开所有必要的 setters/getters,因为它的 values/state 将被 DialogFragment.

操纵

如果你确实走这条路,那么为什么不以相同的方式获取(运动指标的)初始数据 - 通过传递对空白对象的引用 - 除非有其他原因需要初始获取的特殊情况没有提到的数据?这样,您获取和编辑数据的机制将是相同的。

这里有一篇关于通过 bundle/parcel 传递对象引用的好文章 https://medium.com/@hamidgh/sending-objects-to-fragment-naive-question-is-it-sent-by-value-ddaaa19fa42d

嵌入 将允许用户就地编辑数据,无需弹出窗口,但这可能需要比可用的更多 space,因为data/fields 必须足够大才能轻松编辑。

然而,使用你已有的东西,有什么理由不能简单地return编辑的数据,就像最初输入时所做的那样,你似乎在使用Bundle 正确地将值发送回对话框进行编辑,所以我看不出问题吗?