0

I dont know how to do this the right way. I have a master/detail flow, now I perform an action in the detail that needs to finish it, but of course, detail fragment should be unaware of wether its wrapped by a single or dual pane activity.

So how should I do this? I was thinking about a callback that both activities would implement and the single pane would just finish it self and therefore finishing the fragment, and the dual pane would just pop the fragment.

Is this a good idea? Having callbacks from the detail fragment? Isnt this much overhead?

Thanks

urSus
  • 12,492
  • 12
  • 69
  • 89
  • Isn't this much overhead? -> I don't believe so. If you abstract that callback it can be reused for other fragments. I also believe this is a valid suggestion to solve your problem. Fragments introduced flexibility for the developer (more than activities do) but you will have the code more than with activities to handle all use cases. – Tobrun Feb 21 '13 at 15:46
  • No its not an overhead. Its the right way. If you don't want to implement that interface then just do this `if getActivity() instanseOf SinglePaneActivity` then `getActivity().finish();` else `getActivity().getFragmentManager().popMyFragment();`. – M-Wajeeh Feb 21 '13 at 15:46

2 Answers2

5

No its not an overhead. Its the right way.

or do this in your DetailFragment:

if (getActivity() instanseOf SinglePaneActivity){
    getActivity().finish(); 
}else{ 
    getActivity().getFragmentManager().popBackStack();
}
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
2

now I perform an action in the detail that needs to finish it

That is unusual flow for the master/detail pattern. Normally, the detail persists until the user taps on something else in the master list. I could see your proposed flow for a delete operation on the item being viewed, though.

I was thinking about a callback that both activities would implement and the single pane would just finish it self and therefore finishing the fragment, and the dual pane would just pop the fragment.

That is a fine answer.

Isnt this much overhead?

No. Define an interface that all activities hosting your fragment must implement. You might consider using the contract pattern to help enforce that:

import android.app.Activity;
import com.actionbarsherlock.app.SherlockFragment;

// from https://gist.github.com/2621173

public abstract class ContractFragment<T> extends SherlockFragment {
  private T mContract;

  @SuppressWarnings("unchecked")
  @Override
  public void onAttach(Activity activity) {
    try {
      mContract=(T)activity;
    }
    catch (ClassCastException e) {
      throw new IllegalStateException(activity.getClass()
                                              .getSimpleName()
          + " does not implement "
          + getClass().getSimpleName()
          + "'s contract interface.", e);
    }
    super.onAttach(activity);
  }

  @Override
  public void onDetach() {
    super.onDetach();
    mContract=null;
  }

  public final T getContract() {
    return mContract;
  }
}

(code based on a gist from Jake Wharton of ActionBarSherlock fame)

Here, T is the name of the interface. Your fragment would inherit from this and call getContract() to retrieve the interface implementation object, on which you call your callback method. Runtime overhead will be on the order of a few dozen instructions at most -- nothing you will need to worry about.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I was thinking about having a master/detail, with Add/Edit action on the list, that would put a form fragment to where the detail fragment was, so when you submit the form, it would kill it self and went back to the normal detail fragment (tablet). Deleting from the detail fragment is also the case. I just didnt know wether do it with callbacks so wrapper activities control the fragment, or just an if statement inside fragment to figure out who is hosting me, as M-WaJeEh proposed. – urSus Feb 21 '13 at 16:32
  • 1
    @VlastoBennyLava: Ah, OK, your plan makes sense. Personally, I prefer to keep my fragments ignorant of the hosting activity, and the `instanceof` approach requires fragments to know about specific activity classes. Hence, I prefer the interface approach outlined in my answer. Both will work, though. – CommonsWare Feb 21 '13 at 16:34
  • On a another topic, could you please take a look at my other question? http://stackoverflow.com/questions/14990269/where-to-handle-cab-actions-of-a-list-item-that-are-common-with-detail. Thanks. – urSus Feb 21 '13 at 16:39