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

  • SEARCH
  • Home
  • 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 9243123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:44:23+00:00 2026-06-18T08:44:23+00:00

i am terribly fed up because of this. Am trying to invoke a method

  • 0

i am terribly fed up because of this.
Am trying to invoke a method in the SherlockFragment from a SherlockFragmentActivity ..
My SherlockFragmentActivity uses Sherlock’s Tabs and pager features.
Am unable to get reference to my SherlockFragment.
I would be very grateful if you could help me on how to invoke a method present in the fragment from its activity.

This is my SherlockFragmentActivity!

public class main extends SherlockFragmentActivity implements 
menu_fragment.OnMenuSelectedListener,
content_fragment.OnContactSelectedListener{
TabHost mTabHost;
ViewPager mViewPager;
TabsAdapter mTabsAdapter;

public static class TabsAdapter extends FragmentPagerAdapter implements     TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener
{
private final Context mContext;
private final TabHost mTabHost;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private final ActionBar mActionBar;

static final class TabInfo {
    private final String tag;
    private final Class<?> clss;
    private final Bundle args;

    TabInfo(String _tag, Class<?> _class, Bundle _args) {
        tag = _tag;
        clss = _class;
        args = _args;
    }
}

static class DummyTabFactory implements TabHost.TabContentFactory {
    private final Context mContext;

    public DummyTabFactory(Context context) {
        mContext = context;
    }

    @Override
    public View createTabContent(String tag) {
        View v = new View(mContext);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }
}

    public TabsAdapter(SherlockFragmentActivity activity, TabHost tabHost, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mTabHost = tabHost;
        mViewPager = pager;
        mTabHost.setOnTabChangedListener(this);
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
        mActionBar = activity.getSupportActionBar();
    }

    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args){
        tabSpec.setContent(new DummyTabFactory(mContext));
        String tag = tabSpec.getTag();
        Log.d("NET","tabtag: "+tag);
        TabInfo info = new TabInfo(tag, clss, args);
        mTabs.add(info);
        mTabHost.addTab(tabSpec);
        notifyDataSetChanged();
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int position) {
        // Unfortunately when TabHost changes the current tab, it kindly
        // also takes care of putting focus on it when not in touch mode.
        // The jerk.
        // This hack tries to prevent this from pulling focus out of our
        // ViewPager.
        TabWidget widget = mTabHost.getTabWidget();
        int oldFocusability = widget.getDescendantFocusability();
        widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        mTabHost.setCurrentTab(position);
        widget.setDescendantFocusability(oldFocusability);
    }

    @Override
    public void onTabChanged(String arg0) {
        int position = mTabHost.getCurrentTab();
        mViewPager.setCurrentItem(position);
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }

}



@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    setContentView(R.layout.main);
    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();

    mViewPager = (ViewPager) findViewById(R.id.pager);

    mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);     

    mTabsAdapter.addTab(mTabHost.newTabSpec("MENU").setIndicator("MENU"),
            menu_fragment.class, null);
    mTabsAdapter.addTab(mTabHost.newTabSpec("CONTENT").setIndicator("CONTENT"),
            content_fragment.class, null);

    content_fragment c = (content_fragment) getSupportFragmentManager().findFragmentByTag(makeFragmentName(R.id.pager, 1));

    //Log.d("NET",getFragmentTag(1));
    //Log.d("NET",makeFragmentName(R.layout.contacts, 1));

    try{
        c.test();
    }catch(Exception e){
        Log.d("NET",e.toString());
    }
}

private String getFragmentTag(int pos){
    return "android:switcher:"+R.id.pager+":"+pos;   //fragmentpageradapter auto generated tag
}

private static String makeFragmentName(int viewId, int index) {
     return "android:switcher:" + viewId + ":" + index;
}

@Override
public void onMenuSelected(int i){
    Toast.makeText(getApplicationContext(), "menu selected "+i, Toast.LENGTH_SHORT).show();

}

@Override
public void onContactSelected(){
    Toast.makeText(getApplicationContext(), "contact selected", Toast.LENGTH_SHORT).show();
    //ActionBar mActionBar = getSupportActionBar();
}

}

This is my SherlockFragment!

public class content_fragment extends SherlockFragment{
static ScrollView scroller_page;
static String[] contacts = {"gp","janu","tn","varun","joseph"};
OnContactSelectedListener cListener;

public interface OnContactSelectedListener {
    public void onContactSelected();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View f = inflater.inflate(R.layout.content_fragment, container, false);

    return f;
}

public void test(){
    Toast.makeText(getSherlockActivity(), "method called ", Toast.LENGTH_SHORT).show();
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try{
        cListener = (OnContactSelectedListener) activity;
    }catch(Exception e){
        throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
    }
}

}

  • 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-18T08:44:24+00:00Added an answer on June 18, 2026 at 8:44 am

    Try replacing:

    content_fragment c = (content_fragment) getSupportFragmentManager().findFragmentByTag(makeFragmentName(R.id.pager, 1));
    

    with:

    content_fragment c = (content_fragment) mTabsAdapter.getItem(1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize this is not terribly different from a lot of other questions that
I'm getting terribly slow results from the following code. I've been trying to troubleshoot
[edit]: I'm terribly sorry for my timing on asking this question. I've just discovered
This is terribly ugly: psData = [] nsData = [] msData = [] ckData
I'm terribly new to web development. I'm trying to make a pretty simple site
This may be a terribly uninformed question, brace yourself. A company I'm working with
This could well be a terribly ignorant question, if so please forgive me: I'm
I've got to convert a not terribly complicated bespoke project management system from MsAccess
Terribly worded question above (trying to be short). Lets try that again: In Microsoft
This may be a terribly naive question but I was just wondering if the

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.