- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我尝试从 DialogFragment
实现回调。有一个很好的例子,但他们不会从 Fragment
打开此 DialogFragment
。 http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents
这是我的代码:
public class EditDateDialogFragment extends DialogFragment {
// Use this instance of the interface to deliver action events
EditDateDialogListener mListener;
/* The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
public interface EditDateDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
public static EditDateDialogFragment newInstance( int currentCategoryId ) {
EditDateDialogFragment p = new EditDateDialogFragment();
Bundle args = new Bundle();
args.putInt("currentRecordId", currentCategoryId);
p.setArguments(args);
return p;
}
@Override
public void onCreate(Bundle savedInstanceState) {
mCurrentRecordId = getArguments().getInt("currentRecordId");
super.onCreate(savedInstanceState);
}
public void onAttach(SherlockActivity activity) {
super.onAttach(activity);
try {
// Instantiate the EditDateDialogListener so we can send events to the host
mListener = (EditDateDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString() + " must implement EditDateDialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
final View v = inflater.inflate(R.layout.fragment_dialog_edit_date, null);
return new AlertDialog.Builder(getActivity()).setTitle("Set Date...").setView(v).setCancelable(true).setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("", "Dialog confirmed");
mListener.onDialogPositiveClick(EditDateDialogFragment.this);
}
}).setNegativeButton("Abort", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("", "Dialog abort");
dialog.cancel();
}
}).create();
}
}
在 RecordDetailFragment.java 中,我实现了接口(interface)并以这种方式创建了 EditDateDialogFragment 的新实例(只是重要部分):
public class RecordDetailFragment extends SherlockFragment implements EditDateDialogFragment.EditDateDialogListener {
...
DialogFragment editDateFragment = EditDateDialogFragment.newInstance( recordId );
editDateFragment.show(getActivity().getSupportFragmentManager(), "EditDateDialogFrame");
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
LOGD(TAG, "Overriden Dialog confirmed");
//((EditDateDialogFragment) dialog).mDatePicker;
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
// TODO Auto-generated method stub
}
...
}
现在 EditDateDialogFragment
中的 public void onAttach(SherlockActivity activity)
永远不会被调用,因为我从 创建了
而不是 DialogFragment
FragmentActivity
?如何解决这个问题?
更新:在 RecordDetailFragment 中,我将其插入到 onCreate()
if (savedInstanceState != null) {
EditDateDialogFragment dpf = (EditDateDialogFragment) getActivity().getSupportFragmentManager().findFragmentByTag("EditDateDialogFragment");
if (dpf != null) {
dpf.setListener((EditDateDialogListener) this);
}
}
我将 DialogFragment 的实例化更改为
EditDateDialogFragment editDateFragment = EditDateDialogFragment.newInstance( recordId );
editDateFragment.setListener((EditDateDialogListener) this);
editDateFragment.show(getActivity().getSupportFragmentManager(), "EditDateDialogFragment");
请注意 EditDateDialogFragment 而不是 DialogFragment。我不确定如何更新对话框中的引用。
最佳答案
刚跳进同样的问题,解决方法很简单。而不是覆盖
public void onAttach(Context context) {}
覆盖这个:
public void onAttach(Activity activity) {}
DialogFragment
现在一切正常。
关于android - onAttach() 从未在 DialogFragment 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14070297/
我的 on 有问题 附上它在他身上做红线并写信给我: “覆盖‘android.support.v4.app.Fragment’中已弃用的方法” 请帮助我理解我做错了什么?为所有的 helper 打交道
我正在尝试使用 API 23 为 Android 应用程序创建一个 DialogFragment。我发现 onAttach 方法已针对此 API 更新,现在需要一个 上下文 作为参数。问题是,我无法让
我正在编写测试,其中 onAttach 生命周期方法中有一些逻辑,但在测试时永远不会调用 onAttach。 这就是我最初尝试测试它的方式 beforeEach(() => { vi
Marionette 版本3.2.0 onAttach 生命周期事件未触发 根据doc The events marked with "*" only fire if/when the region'
在创建 fragment 时,我遇到 getActivity() 为空。因此,为了缩小问题的范围,我在 onAttach(Activity activity) 中保留了一个 Activity 的本地副
我创建的 fragment 取决于其父 Activity 的一些属性。我浏览了 Fragment Life cycle文件。我需要将 Activity 的副本存储在一个变量中,以便以后可以访问它。有两
我的 Fragment 从 AppCompatActivity 启动时没有调用 onAttach(context) 方法。 在 XML 中创建 fragment : 但如果我从 support.v4
我更新了我的应用程序以使用最新的支持库(版本 23.0.0),我发现他们弃用了 Fragment 类的 onAttach() 函数。 代替: onAttach (Activity activity)
所以我有这个角 Dart 组件: @Component(...) class TableCard implements AttachAware, ShadowRootAware { void on
我试图在 fragment 中获取 Activity ,因为我在 getActivity.finish() 上不断收到 空指针异常。 我正在尝试通过以下方式实现这一目标: @Override publ
我创建了一个接口(interface) DataPasserInterface 将数据从 fragment 传递到其托管 Activity 。我在 onAttach 中初始化了界面,如下代码所示,问题
我正在尝试使用类型化回调实现抽象 fragment ,以便在多个子类中使用它。 如何检查 Context 是否是适当类的实例? 我的abstact CallbackFragment代码: public
有什么方法可以保证 onAttach 正常工作吗? @Override public void onAttach(Activity activity) { super.onAttach(act
我有一个自定义类,其父类是 SurfaceView。我让类(class)正常工作,从它自己的线程等绘制到屏幕上。但是我想了解应该如何正确处理问题标题中的方法。 例如,如果我的应用程序正在运行(在手机上
在 onAttach 函数中,eclipse 显示错误说明 The method onAttach(Activity) in the type Fragment is not applicable f
我尝试从 DialogFragment 实现回调。有一个很好的例子,但他们不会从 Fragment 打开此 DialogFragment。 http://developer.android.com/g
将 SDK 更新到 API 级别 23 后,我发现 onAttach (Activity activity) 已弃用,新方法是 onAttach (Context context)。谁能告诉我为什么要
在 Fragment 的生命周期中,onAttach() 方法在 onCreate() 方法之前被调用。我无法解决这个问题。为什么要先附加 Fragment? 最佳答案 TL;DR: 为了不破坏 an
我知道这是 nob 问题,但我无法理解变量 someEventListener 赋值什么?..我们如何将 Activity 与接口(interface) onSomeEventListener) 进行
所以我有一个奇怪的问题,我现在无法解决。 房间游戏 fragment @Override public void onAttach(Activity activity) { super.onA
我是一名优秀的程序员,十分优秀!