SamFast
SamFast

Reputation: 1054

How can activity call a method inside fragment created dynamically

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.

Upvotes: 0

Views: 810

Answers (2)

Naveen T P
Naveen T P

Reputation: 7275

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

Upvotes: 0

zozelfelfo
zozelfelfo

Reputation: 3776

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

Upvotes: 1

Related Questions