android nested fragment doesn't work properly

416 Views Asked by At

I'm trying to show a nested fragment inside my fragment on button click, so on button .setOnClickListener (which works fine with Log.d) of my main fragment i've placed:

    Fragment noteFragment = new FrontPageNote();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.frontNote, noteFragment).commit();

as found on developer guide, where FrontPageNote is my nested fragment (which obviously extends Fragment) and R.id.frontNote is the id of the xml layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="48dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:layout_weight="1"
    android:id="@+id/frontNote"
    android:background="@color/Background">

    <LinearLayout
        android:id="@+id/linearLayoutFront"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp"
        android:paddingTop="15dp">

        <EditText
            android:id="@+id/titleEditFront"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/titleEdit"
            android:textColor="@color/TextColor.White"/>

        <EditText
            android:id="@+id/noteEditFront"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/noteEdit"
            android:paddingTop="20dp"
            android:textColor="@color/TextColor.White"
            android:inputType="textMultiLine" />
    </LinearLayout>

</RelativeLayout>

the nested fragment FrontPageNote also is linked to this xml using getActivity().setContentView(R.layout.front_note); under onCreate() method.

Q. what am I doing wrong? thanks


EDIT: added FrontPageNote.class:

public class FrontPageNote extends Fragment {

// Declare Variables
public static long rowID;
private EditText title_edit;
private EditText note_edit;
private static final String TITLE = "title";
private static final String NOTE = "note";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    getActivity().setContentView(R.layout.front_note);

    // Locate the EditText in add_note.xml
    title_edit = (EditText) getActivity().findViewById(R.id.titleEditFront);
    note_edit = (EditText) getActivity().findViewById(R.id.noteEditFront);

    // Retrieve the Row ID from ViewNote.java
    Bundle extras = getActivity().getIntent().getExtras();
    if (extras != null) {
        rowID = extras.getLong("row_id");
        title_edit.setText(extras.getString(TITLE));
        note_edit.setText(extras.getString(NOTE));
    }
}

// Create an ActionBar menu
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_all_geofences, menu);
    menu.add("Save changes")
            .setOnMenuItemClickListener(this.SaveButtonClickListener)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    super.onCreateOptionsMenu(menu, inflater);
}

// Capture save menu item click
OnMenuItemClickListener SaveButtonClickListener = new OnMenuItemClickListener() {
    public boolean onMenuItemClick(MenuItem item) {

        // Passes the data into saveNote() function
        if (title_edit.getText().length() != 0) {
            AsyncTask<Object, Object, Object> saveNoteAsyncTask = new AsyncTask<Object, Object, Object>() {
                @Override
                protected Object doInBackground(Object... params) {
                    saveNote();
                    return null;
                }

                @Override
                protected void onPostExecute(Object result) {
                    // Close this activity
                    //getActivity().finish();
                }
            };
            // Execute the saveNoteAsyncTask AsyncTask above
            saveNoteAsyncTask.execute((Object[]) null);
        }

        else {
            // Display a simple alert dialog that forces user to put in a title
            AlertDialog.Builder alert = new AlertDialog.Builder(
                    getActivity());
            alert.setTitle("Title is required");
            alert.setMessage("Put in a title for this note");
            alert.setPositiveButton("Okay", null);
            alert.show();
        }

        return false;

    }
};
// saveNote() function
private void saveNote() {
    DatabaseConnector dbConnector = new DatabaseConnector(getActivity());

    if (getActivity().getIntent().getExtras() == null) {
        // Passes the data to InsertNote in DatabaseConnector.java
        dbConnector.InsertNote(title_edit.getText().toString(), note_edit
                .getText().toString());
    } else {
        // Passes the Row ID and data to UpdateNote in DatabaseConnector.java
        dbConnector.UpdateNote(rowID, title_edit.getText().toString(),
                note_edit.getText().toString());
    }
}

}

0

There are 0 best solutions below