PDA

View Full Version : Unable to Call Method from Parent Activity



SheilaG
03-19-2014, 03:59 PM
I am creating an AlertDialog with single choice (radio buttons). My dialog title, and text are dynamically created depending on which button the user clicks in from the parent FragmentActivity. Then, if the user clicks the positive button (in my case, save), I'd like the string that corresponds to his choice to be passed back to the parent FragmentActivity, to be displayed in the corresponding TextView.
This is myDialogFragment code:



package com.myDummyProject.FoodSelector;


import java.util.Arrays;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.Toast;




public class DummyDialogFragment extends DialogFragment {

public interface selectChoiceDialogListener {
public void onDialogPositiveClick(DialogFragment dialog,String dialogSubject,CharSequence selectedChoice);
}

selectChoiceDialogListener clickListener;

public static CharSequence[] radioOptions;
public static CharSequence selectedChoice = new String();
public static String dialogSubject = new String();
public static String dialogTitle = new String();
public static CharSequence defaultChoice = new String();


public DummyDialogFragment(){

}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

// Retrieve values from bundle
radioOptions = getArguments().getCharSequenceArray("DIALOG_radioOptions");
dialogSubject = getArguments().getString("CHOICE_TYPE");
dialogTitle = getArguments().getString("DIALOG_TITLE");
defaultChoice = getArguments().getString("DEFAULT_CHOICE");
int defaultValue = Arrays.asList(radioOptions).indexOf(defaultChoice) ;
selectedChoice = defaultChoice;

// Build the dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(dialogTitle)

.setSingleChoiceItems(radioOptions, defaultValue, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectedChoice = radioOptions[which];
}
})

.setPositiveButton(R.string.action_save_allcaps, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Save selectedChoice; will be retrieved and showed in TextView of previous activity
// ==> THIS DOESN'T SEEM TO BE WORKING <==
clickListener.onDialogPositiveClick(DummyDialogFra gment.this,dialogSubject,selectedChoice);

// Toast informing user that choice was saved
CharSequence toastMessage = dialogSubject + " saved as " + selectedChoice;
Toast.makeText(getActivity(),toastMessage, Toast.LENGTH_SHORT).show();


// TODO: Return to previous activity -> make sure new values appear in the activity

}
})
.setNegativeButton(R.string.action_cancel_allcaps, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {

// Toast informing user that choice was cancelled
CharSequence toastMessage = dialogSubject + " change was cancelled";
Toast.makeText(getActivity(),toastMessage, Toast.LENGTH_SHORT).show();

}
});

// Create the dialog
return builder.create();
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
clickListener = (selectChoiceDialogListener) activity;
}


}


I've indicated what I believe is not working with the comment //==> THIS DOESN'T SEEM TO BE WORKING <==.
And this is my parent FragmentActivity code:


package com.myDummyProject.FoodSelector;


import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.annotation.TargetApi;
import android.support.v4.app.FragmentTransaction;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.FragmentActivity;


public class DummyParentFragment extends FragmentActivity implements DummyDialogFragment.selectChoiceDialogListener {


(...)


public void pickFruits (View view){

DialogFragment pickFoodsDialog = new DummyDialogFragment();

String dialogTitle = "Select a fruit:";
String dialogSubject = "Fruit";
CharSequence[] radioOptions = new CharSequence[]{"Apple","Banana","Strawberry"};
TextView tv_fruit = (TextView) findViewById(R.id.textView_tv_fruit);
CharSequence defaultFruit = tv_fruit.getText();

Bundle extras = new Bundle();
extras.putCharSequenceArray("DIALOG_radioOptions", radioOptions);
extras.putString("DIALOG_TITLE", dialogTitle);
extras.putString("DIALOG_SUBJECT", dialogSubject);
extras.putCharSequence("DEFAULT_CHOICE", defaultFruit);
pickFoodsDialog.setArguments(extras);

pickFoodsDialog.show(getSupportFragmentManager(),"pickFoodsDialog");
}

public void selectDessert(View view){

DialogFragment pickFoodsDialog = new DummyDialogFragment();

String dialogTitle = "Select a dessert:";
String dialogSubject = "Dessert";
CharSequence[] radioOptions = new CharSequence[]{"Cheescake","Chocolate Cake","Ice Cream","Creme Brulee"};
TextView tv_dessert = (TextView) findViewById(R.id.textView_day);
CharSequence defaultDayOfWeek = dayOfWeek.getText();

Bundle extras = new Bundle();
extras.putCharSequenceArray("DIALOG_radioOptions", radioOptions);
extras.putString("DIALOG_TITLE", dialogTitle);
extras.putString("DIALOG_SUBJECT", dialogSubject);
extras.putCharSequence("DEFAULT_CHOICE", defaultDayOfWeek);

pickFoodsDialog.setArguments(extras);
pickFoodsDialog.show(getSupportFragmentManager(),"pickFoodsDialog");
}

public void selectStarter (View view){

DialogFragment radioButtonsDialog = new DummyDialogFragment();

String dialogTitle = "Select a starter:";
String dialogSubject = "Starter";
CharSequence[] radioOptions = new CharSequence[]{"Soup of the day","Garlic Bread","Prawns with garlic sauce","Mussels in white wine"};
TextView reminder = (TextView) findViewById(R.id.textView_reminder);
CharSequence defaultReminder = reminder.getText();

Bundle extras = new Bundle();
extras.putCharSequenceArray("DIALOG_radioOptions", radioOptions);
extras.putString("DIALOG_TITLE", dialogTitle);
extras.putString("DIALOG_SUBJECT", dialogSubject);
extras.putCharSequence("DEFAULT_CHOICE", defaultReminder);

radioButtonsDialog.setArguments(extras);
radioButtonsDialog.show(getSupportFragmentManager( ),"pickFoodsDialog");
}

public void onDialogPositiveClick(DialogFragment dialog, String dialogSubject, CharSequence selectedChoice) {
// User touched the dialog's positive button
TextView tv = new TextView(this);
if(dialogSubject.equals("Fruit")){
tv = (TextView)findViewById(R.id.textView_tv_fruit);
}else if (dialogSubject.equals("Dessert")){
tv = (TextView)findViewById(R.id.textView_dessert);
}else if (dialogSubject.equals("Starter")){
tv = (TextView)findViewById(R.id.textView_starter);
}
tv.setText(selectedChoice);
}


}







When I press the SAVE button, the toast message displays fine, and then the dialog closes, however I think that the method onDialogPositiveCLick is not being called in the parent FragmentActivity, since the TextViews never change. I thought this would be managed by the clickListener (calling the method).

I should say I posted the same question in StackOverflow here (http://stackoverflow.com/questions/22512201/cannot-call-parent-fragment-method-from-dialog-fragment). Someone suggested that my parent Activity needs to be refreshed, however I cannot fully understand how to implement their solution. As the level of expertise in SO is quite high I decided to look for a beginners forum, and here I am. In SO you'll find an updated version of the code above, with a failed attempt to implement the solution they proposed.

Thanks so much!