:
onAttach(Activity) called once the fragment is associated with its activity.
mListener = (SetupHotspotDialogListener) activity;
ClassCastException SetupHotspotDialogListener. (Fragment , , DialogFragment DialogFragment Fragment).
, . - , . , .
, FragmentDialog Fragment :
Fragment SmartMode ( dialogFragment) , createDialogRequest().- , ,
Fragment Activity - " "
Activity
, .
: , , . fragment dialog fragment dialog .
:
public class MyFragmentActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_fragment);
if (savedInstanceState == null) {
MyFragment fragment = new MyFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragmentContainer, fragment, "fragment");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
}
}
:
public class MyFragment extends Fragment implements MyDialogInterface {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View fragmentView = inflater.inflate(R.layout.fragment, null);
Button showDialogButton = (Button) fragmentView.findViewById(R.id.showDialog);
showDialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentDialog dialog = FragmentDialog.getInstance(MyFragment.this);
dialog.show(getActivity().getSupportFragmentManager(), "dialog");
}
});
return fragmentView;
}
@Override
public void onClickEvent() {
Log.e(getClass().getSimpleName(), "onClickEvent");
}
}
FragmentDialog:
public class FragmentDialog extends DialogFragment {
public interface MyDialogInterface extends Serializable {
public void onClickEvent();
}
private MyDialogInterface callbackListener;
public static FragmentDialog getInstance(MyDialogInterface dialogInterface) {
FragmentDialog fragmentDialog = new FragmentDialog();
Bundle args = new Bundle();
args.putSerializable("dialogInterface", dialogInterface);
fragmentDialog.setArguments(args);
return fragmentDialog;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View pushDialogView = getActivity().getLayoutInflater().inflate(R.layout.fragment_dialog, null);
callbackListener = (MyDialogInterface) getArguments().getSerializable("dialogInterface");
Button cancelButton = (Button) pushDialogView.findViewById(R.id.button);
cancelButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
callbackListener.onClickEvent();
}
});
return pushDialogView;
}
}
4
android.support.v4.app.Fragment
android.support.v4.app.DialogFragment
android.support.v4.app.FragmentActivity
:
activity_my_fragment.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#a00"
android:orientation="vertical" >
<Button
android:id="@+id/showDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show doalog" />
</LinearLayout>
fragment_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fe3"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click me" />
</LinearLayout>
, , .