0

I have a main activity which creates fragments using the following code

private void launchFragment(int pos)
{
    Fragment f = null;
    String title = null;
    if (pos == 1)
    {
        title = "Friends";
        f = new FriendList();
    }
    else if (pos == 2)
    {
        title = "Notes";
        f = new NoteList();
    }
    else if (pos == 3)
    {
        title = "Projects";
        f = new ProjectList();
    }
    else if (pos == 5)
    {
        title = "About";
        f = new AboutUs();
    }
    else if (pos == 6)
    {
        startActivity(new Intent(this, Login.class));
        finish();
    }
    if (f != null)
    {
        while (getSupportFragmentManager().getBackStackEntryCount() > 0)
        {
            getSupportFragmentManager().popBackStackImmediate();
        }
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, f).addToBackStack(title)
                .commit();
    }
}

Here is the code of a fragment.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    View v = inflater.inflate(R.layout.group_chat, null);

    loadConversationList();

    contactName = this.getArguments().getString("contactusername");

    contactId = this.getArguments().getString("contactid");


    ListView list = (ListView) v.findViewById(R.id.list);
    adp = new ChatAdapter();
    list.setAdapter(adp);
    list.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    list.setStackFromBottom(true);

    txt = (EditText) v.findViewById(R.id.txt);
    txt.setInputType(InputType.TYPE_CLASS_TEXT
            | InputType.TYPE_TEXT_FLAG_MULTI_LINE);

    setTouchNClick(v.findViewById(R.id.btnCamera));
    setTouchNClick(v.findViewById(R.id.btnSend));
    return v;
}

I want to call a method in above fragment class. I was not able to do this as I have not given id of the fragment in the XML file. I am not loading the static fragment using XML. Therefore, I don't have Id.

I have already seen this and this questions on the StackOverFlow, but they are not solving my problem.

Kindly help if anyone knows how to tackle this scenario.

Community
  • 1
  • 1
SamFast
  • 1,054
  • 2
  • 16
  • 31
  • Do you have that mathod in all fo your fragments or you want to call that method only in one of those? – Hellboy Nov 15 '14 at 11:05

2 Answers2

1

First of all make all your fragments implement an interface. This interface will return a String (for example) which will identify your fragment, and then cast your fragment to it after getting the fragment using findFragmentById() as follows:

Create your interface

 public interface IFragmentName
 {
    public String getFragmentName();
 }

Implement your interface (for example in NoteList)

public NoteList extends Fragment implements IFragmentName
{
   //Do your stuff...
   public String getFragmentName()
   {
       return "NoteList";
   }

}

After this get your current fragment from your activity

IFragmentName myFragment = (IFragmentName) getSupportFragmentManager().findFragmentById(R.id.content_frame);

Finally check your getFragmentName() value and cast to the fragment you want:

if(myFragment.getFragmentName().equals("NoteList")
{
   NoteList myNoteListFragment = (NoteList) myFragment;
   myNoteListFragment.callMyMethod(); //here you call the method of your current Fragment.
}

I have coded these snippets without any IDE so maybe I have missed a semicolon or something like that :)

Hope it helps

zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
  • Thanks, you are super fast... let me try them in a moment... Thanks :) – SamFast Nov 15 '14 at 11:51
  • It was superb man, thanks again... I searched lot of Internet but could not find this solution... I marked the answer as best and also gave you a vote :) – SamFast Nov 15 '14 at 14:22
  • There is one problem why does it returns null to myFragment... I checked in docs, it said getFragmentById returns fragment or null, why it is not able to find the content_frame fragment. – SamFast Nov 15 '14 at 15:12
  • In my xml of activity, content_frame is id of the FrameLayout, it is used to put the fragments there – SamFast Nov 15 '14 at 15:22
  • sorry, i just realized that I am using getSupportFragmentManager() instead of getFragmentManager(). Kindly, also update the answer so that anyone else will not get into confusion... – SamFast Nov 15 '14 at 15:41
0

I know I am too late for the party. But this will be useful for others.

If at all you are using ViewPager to render the fragment use this code in your parent Activity.

Check Solution here

Community
  • 1
  • 1
Naveen T P
  • 6,955
  • 2
  • 22
  • 29