Reputation: 3111
I'm developing an app using Fragments and I have an ImageButton on one of my Fragments. It's supposed to ask the user if they want to save what they've entered in an EditText and then save it to a SQLiteDatabase if they press enter. This all worked fine when this Fragment was an Activity, except that I didn't have to override onKeyDown. Now that I've moved it to a Fragment, the ImageButton does nothing. What am I missing? Here's the code:
<EditText
android:id="@+id/stitchnotes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:hint="@string/hint"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/white"
android:inputType="text" />
<ImageButton
android:id="@+id/savebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/video_content_description"
android:src="@drawable/actions_file_save_as_icon" />
I'm using the technique from here to override the onKeyDown method in the Activity that extends Fragment Activity:
public class StitchList extends FragmentActivity {
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
super.onKeyDown(keyCode, event);
DetailFrag frag = new DetailFrag();
frag.onKeyDown(keyCode, event);
return false;
}
and then defining onKeyDown in my Fragment:
public class DetailFrag extends Fragment {
public static boolean Edited = false;
public AlertDialog.Builder builder;
public AlertDialog alert;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.detailfragment, container, false);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(),
"danielbk.ttf");
builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Do you want to save your Notes?");
builder.setCancelable(false);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getActivity().finish();
}
});
alert = builder.create();
final EditText notes = (EditText)view.findViewById(R.id.stitchnotes);
notes.setTypeface(font);
notes.setTextSize(12);
ImageButton savebutton = (ImageButton)view.findViewById(R.id.savebutton);
savebutton.setOnClickListener(new ImageButton.OnClickListener() {
public void onClick(View v) {
String Notes = notes.getText().toString();
Uri updateUri = ContentUris.withAppendedId(STITCHES_URI, rowID);
ContentValues values = new ContentValues();
values.put("stitchnotes", Notes);
getActivity().getContentResolver().update(updateUri,values,null,null);
}
});
notes.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
Edited = true;
}
});
return view;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0
&& Edited) {
alert.show();
return true;
}
else
return false;
}
Upvotes: 0
Views: 1599
Reputation: 7478
Right now in your onKeyDown
method in the FragmentActivity
it is going to create a new Fragment every time a key is pressed, but it never adds it to the FragmentManager
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
super.onKeyDown(keyCode, event);
// Creates a new DetailFrag
DetailFrag frag = new DetailFrag();
// Call the onKeyDown in the new Fragment
frag.onKeyDown(keyCode, event);
return false;
}
You want to get the existing DetailFrag
from the FragmentManager
, something like:
DetailFrag f = (DetailFrag) getFragmentManager().findFragmentByTag("MyTag");
or
DetailFrag f = (DetailFrag) getFragmentManger().findFragmentById(R.id.fragment);
once you have the fragment you can call the onKeyDown
method.
Also, in your DetailFrag
if you want to pop up the alert when the ImageButton
is clicked you need to call alert.show()
in the onClick
method.
savebutton.setOnClickListener(new ImageButton.OnClickListener() {
public void onClick(View v) {
alert.show();
....
}
});
And then the methods that update your database would go in the onClick
method of the PositiveButton
in the Dialog
, right now it is just calling dialog.cancel()
which just dis
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String Notes = notes.getText().toString();
Uri updateUri = ContentUris.withAppendedId(STITCHES_URI, rowID);
ContentValues values = new ContentValues();
values.put("stitchnotes", Notes);
getActivity().getContentResolver().update(updateUri,values,null,null);
}
});
Upvotes: 2