vlio20
vlio20

Reputation: 9295

showing a FragmmentDialog from calls that extends View class

I have an activity which is called MainPage and it extends SherlockFragmentActivity. This activity has tabs, each tab shows different fragment. One of the fragments displays a SaleRow view which is a custom view (class that extends the RelativeLayout class). Also I have SaleDialog class which extends DialogFragment. What I am trying to do is to show the SaleDialog from the SaleRow view class. I tried to use this code:

public class SaleRow extends RelativeLayout 
{   
    public SaleRow(Context context) 
    {
        super(context);

        ...

        this.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View view) 
            {
            FragmentManager fm = getFragmentManager(); //compilation error here for getFragmentManager The method getFragmentManager() is undefined for the type new View.OnClickListener()
                SaleDialog testDialog = new SaleDialog();
                testDialog.setRetainInstance(true);
                testDialog.show(fm, "fragment_name");
            }

        });

I have search for a solution but couldn't find something relevant.

Thaks

Upvotes: 1

Views: 1795

Answers (3)

Jason Robinson
Jason Robinson

Reputation: 31283

Trying to affect the state of the fragment manager from a view is a bad idea in my opinion. What I prefer to do is only allow the parent Activity to manipulate the fragment manager. This way, the view is decoupled from the activity, and the activity can decide what it wants to do and how it wants to do it. So I would listen for the click event in the Fragment, then pass that event up to the parent Activity with a callback interface:

The following goes in the Fragment that has the SaleRow:

// Step 1 - Create callback interface
Callback mCallback;

public interface Callback {

    public void showSaleDialog();
}

// Step 2 - Cast the parent Activity to the callback
@Override
public void onAttach(Activity activity) {

    try {
        mCallback = (Callback) activity;
    }
    catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement Callback");
    }
}

// Step 3 - Set the listener on the SaleRow in the onCreateView() class
saleRow.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

        // Step 4 - call showSaleDialog() using the callback
        mCallback.showSaleDialog();
    }
});

And then in the Activity:

public class MyActivity extends SherlockFragmentActivity implements Callback {

    @Override
    public void showSaleDialog() {

        FragmentManager fm = getSupportFragmentManager();
        SaleDialog testDialog = new SaleDialog();
        testDialog.setRetainInstance(true);
        testDialog.show(fm, "fragment_name");
    }

Upvotes: 0

chopchop
chopchop

Reputation: 1933

Try keeping a reference to your context object, cast it and then call getSupportFragmentManager on it:

public class SaleRow extends RelativeLayout 
{
    private Context mContext;
    public SaleRow(Context context) 
    {
        super(context);
        mContext = context;
        this.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View view) 
            {
                try{
                    FragmentManager fm = ((FragmentActivity) mContext).getSupportFragmentManager();
                } catch (ClassCastException e) {
                   Log.d(TAG, "Can't get fragment manager frmom context");
                }
                SaleDialog testDialog = new SaleDialog();
                testDialog.setRetainInstance(true);
                testDialog.show(fm, "fragment_name");
            }

        });
    }
}

Upvotes: 6

Niraj Adhikari
Niraj Adhikari

Reputation: 1728

try SomeActivity.getFragmentManager();

Upvotes: 0

Related Questions