Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8887533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:49:56+00:00 2026-06-14T21:49:56+00:00

The Eclipse generated master detail flow has some callback magic in the class which

  • 0

The Eclipse generated master detail flow has some callback magic in the class which extends ListFragment – I say magic because that’s what it looks like when you are an Android and a Java noob, all at once 🙂

Given the code below can someone answer a few questions for me:

  1. What does the onAttach method of the ListFragment do with mCallbacks = (Callbacks) activity?
  2. Which onItemSelected method is called in onListItemClick method of ListFragment, the one which needs implementation or onItemSelected in FragmentActivity?
  3. All these onItemSelected methods take an id of type String (because DummyContent id is a String). If I changed DummyContent id to a long, which onItemSelected methods would I need to change? I tried changing the one in FragmentActivity but this has @Override so I wasn’t allowed to 🙁

Thank you

public class RecordingListFragment extends ListFragment {

    private static final String STATE_ACTIVATED_POSITION = "activated_position";

    private Callbacks mCallbacks = sDummyCallbacks;
    private int mActivatedPosition = ListView.INVALID_POSITION;

    public interface Callbacks {

        public void onItemSelected(String id);
    }

    private static Callbacks sDummyCallbacks = new Callbacks() {
        @Override
        // NEEDS IMPLEMENTATION - i guess????
        public void onItemSelected(String id) {
        }
    };

...

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (!(activity instanceof Callbacks)) {
            throw new IllegalStateException("Activity must implement fragment's callbacks.");
        }

        mCallbacks = (Callbacks) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = sDummyCallbacks;
    }

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
        super.onListItemClick(listView, view, position, id);
        mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
    }

...

}

And one more file…

public class RecordingListActivity extends FragmentActivity
        implements RecordingListFragment.Callbacks {

    private boolean mTwoPane;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recording_list);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        if (findViewById(R.id.recording_detail_container) != null) {
            mTwoPane = true;
            ((RecordingListFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.recording_list))
                    .setActivateOnItemClick(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemSelected(String id) {
        if (mTwoPane) {
            Bundle arguments = new Bundle();
            arguments.putString(RecordingDetailFragment.ARG_ITEM_ID, id);
            RecordingDetailFragment fragment = new RecordingDetailFragment();
            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.recording_detail_container, fragment)
                    .commit();

        } else {
            Intent detailIntent = new Intent(this, RecordingDetailActivity.class);
            detailIntent.putExtra(RecordingDetailFragment.ARG_ITEM_ID, id);
            startActivity(detailIntent);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:49:57+00:00Added an answer on June 14, 2026 at 9:49 pm

    What does the onAttach method of the ListFragment do with mCallbacks =
    (Callbacks) activity?

    In the onAttach callback the Activity/FragmentActivity(to which the Fragment will be tied/used) is passed to that Fragment instance(through the activity parameter). That line simply casts that Activity to the Callbacks interface to later be used in the Fragment(see below). Your Activity must implement that interface otherwise the code will fail due to the previous if condition. Basically, the code in the onAttach method says: “The Activity where this fragment will be used must implement the Callbacks interface otherwise the code will fail with an IllegalStateException”.

    Which onItemSelected method is called in onListItemClick method of
    ListFragment, the one which needs implementation or onItemSelected in
    FragmentActivity?

    In a very raw explanation: The RecordingListFragment keeps a reference(the mCallbacks field) to a Callbacks type object(meaning a class which implements the Callbacks interface). Initially, to this Callabacks reference the code will assign a default/empty Callbacks reference(sDummyCallbacks) which doesn’t do anything(so no, you don’t have to provide some implementation for the sDummyCallbacks) as its onItemSelected is empty, this is to avoid a possible NullPointerException in some cases(for example, if you don’t assign something to the mCallbacks field and you call onItemSelected on it). When the onAttach method is run the reference to the Activity where this Fragment will exist will be cast to a Callbaks and put in the mCallbacks field. After this happens, when you call mCallbacks.onItemSelected() the FragmentActivity‘s onItemSelected method will be called and the code from the method will be run. If at a later point the onDetach is called, the mCallbacks will once again point to the sDummyCallbacks, after this happens, calling mCallbacks.onItemSelected() will do nothing.

    The interface system above is important because it will make your RecordingListFragment a much more reusable component in your code as it will not be tied to a specific Activity implementation. When the user clicks an item in your fragment’s list you’ll call the onItemSelected on the mCallbacks reference to run the onItemSelected method from that object(the Activity in your case). Your fragment doesn’t know how implements the interface and it doesn’t even care. Think, for example that you have three activities and each one uses a RecordingListFragment fragment. How you would need to change the RecordingListFragment class to make it work with the three activities where it will be used?

    All these onItemSelected methods take an id of type String (because
    DummyContent id is a String). If I changed DummyContent id to a long,
    which onItemSelected methods would I need to change? I tried changing
    the one in FragmentActivity but this has @Override so I wasn’t allowed
    to 🙁

    Modify the interface:

    public interface Callbacks {
    
            public void onItemSelected(long id);
    }
    

    If you save the java file Eclipse will complain(that you must override a superclass method) where this interface was implemented. For the sDummyCallbacks:

    private static Callbacks sDummyCallbacks = new Callbacks() {
        @Override
        // NEEDS IMPLEMENTATION - i guess???? <- it doesn't need no implementation
        // it's purpose is to do nothing
        public void onItemSelected(long id) {
        }
    };
    

    Also the FragmentActivity implements the Callbacks interface so you need to change that too:

    public class RecordingListActivity extends FragmentActivity
            implements RecordingListFragment.Callbacks {
    
        @Override
        public void onItemSelected(long id) {
            if (mTwoPane) {
        // ...
    

    finally you would use the mCallbacks in your fragment:

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {     
        mCallbacks.onItemSelected(id);
    }
    

    I would recommend that you take some time to study a bit about the java language, so you’ll work to understand the Android code and not the Android code + the java way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some auto-generated hibernate DAO code which was generated by Eclipse-hibernate reverse engineering
In eclipse IDE, how can I see the javascript source code GWT has generated
When I create Java Class in Eclipse which implements Serializable interface, I get a
I have a .jar generated by Eclipse, which I cannot run on other computer
I have an APK generated from eclipse which contains a native library packaged inside
In OpenSolaris OS, when I run makefile generated by Eclipse CDT on the Linux
I generated a web-service client using JBoss utils (JAX-WS compatible) using Eclipse 'web service
Please help, I'm going slightly mad!! I'm using Eclipse-generated antfiles to build a project
I generated an apk file for Android version 2.2 with Eclipse => export, then
Under Helios/m2eclipse, when I generated jaxb sources they would be put on the Eclipse

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.