Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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 <code>Activity</code> 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 <code>Fragment</code>, then pass that event up to the parent <code>Activity</code> with a callback interface:</p> <p>The following goes in the <code>Fragment</code> that has the <code>SaleRow</code>:</p> <pre><code>// 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(); } }); </code></pre> <p>And then in the <code>Activity</code>:</p> <pre><code>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"); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload