2

I want to call an fragment method from my activity, describe like in this SO-question: FindByID

TestFragment testFrag = (TestFragment) getFragmentManager().getFragmentById(R.id.testfragment);

if(testFrag != null && testFrag.isAdded())
    testFrag.testMethod("Test");

As seen they identify the fragment by ID which is set in the xml. As I only have a fragment container none of my fragment layouts have a ID.

So my seconds thought was using the function getFragmentByTag `like in Tags

fragmentTransaction.replace(R.id.frameTitle, casinodetailFragment, "fragmentTag");
  • but as I use an action bar with tabs I don't have any tags:

My activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_fragmentcontainer);

  // ActionBar gets initiated
  ActionBar actionbar = getActionBar();
  actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

  // Define Tabs
  ActionBar.Tab BasicInfoTab = actionbar.newTab().setText(
      R.string.tab_BaseInfo);
  ..

  // Define Fragments
  PlantBasicInfoFragment BasicInfoFragment = new PlantBasicInfoFragment();
  ..

  // Set TabListeners
  BasicInfoTab.setTabListener(new TabListener(BasicInfoFragment));
  ..

  // Add tabs to Actionbar
  actionbar.addTab(BasicInfoTab);
  ..

  // Icon clickable
  actionbar.setDisplayHomeAsUpEnabled(true);
}

and its layout:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">

      <LinearLayout 
          android:orientation="horizontal"
          android:id="@+id/LabeledObjectHeaderInfo"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" 
          >


        <!-- ... -->

   </LinearLayout > 


      <!-- fragment container -->
   <LinearLayout
          android:orientation="horizontal"
          android:id="@+id/fragment_container"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          >
   </LinearLayout>

</LinearLayout>

One of my fragments:

<?xml version="1.0" encoding="utf-8"?>

    <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:id="@+id/table">

        <TableRow>
          <!-- ... -->
        </TableRow>

        <!-- ... -->


  </TableLayout>

and finally my tab listener

public class TabListener implements ActionBar.TabListener {

  public Fragment fragment;

  public TabListener(Fragment fragment) {
    this.fragment = fragment;
  }


  @Override
  public void onTabReselected(Tab tab, FragmentTransaction ft) {

  }

  @Override
  public void onTabSelected(Tab tab, FragmentTransaction ft) {

    ft.replace(R.id.fragment_container, fragment);
  }

  @Override
  public void onTabUnselected(Tab tab, FragmentTransaction ft) {

    ft.remove(fragment);

  }

}

So how can I identify my current fragment?

Thanks in advance

P.S. The whole tab switching works perfectly, but now I want to build an addional feature inside the activity!

Community
  • 1
  • 1
bish
  • 3,381
  • 9
  • 48
  • 69

2 Answers2

0

Add a tag while creating tabs.

ActionBar.Tab BasicInfoTab = actionbar.newTab().setText(R.string.tab_BaseInfo);
BasicInfoTab.setTag("tab1");

And in the listener class, retrieve the tag upon event.

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    String tabId = tab.getTag().toString();

}

This way you get the current tag. To identify the current fragment, you can keep a stack of fragments and pick the right one on tab change.

Edit

The tabId, aforementioned, identifies which tab is which. In order to set a fragment id/tag, there are many ways depending on your needs. One method is to replace your actual fragment layout with an intermediate layout.

For instance, A be the fragment you want to add to fragment container. Create another fragment class called IntermediateFragment with the following layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" 
android:baselineAligned="false">

<fragment
    android:id="@+id/fragmentId"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="classA" />

</LinearLayout> 

Now assign the layout you mentioned in the post (containing table) as a new one and add it to fragment class A. Now add Intermediate fragment to container which in turn pulls fragment A. Identify fragment A by id/tag added in the layout.

Hope it makes some sense!

Renjith
  • 3,457
  • 5
  • 46
  • 67
  • How can I access the tabId in my activity when it is set in the tab listener? The method in which I want to access the current tab is in my activit? and the fragment managers always returns `null` when I try to find `findFragmentByTag` after setting a tag like you described. – bish Sep 03 '13 at 18:06
0

Finally I got my solution by changing the TabListener, so I can add the fragment with an tag and then use the findFragmentByTag() function.

So my TabListener now looks like this (only constructor and onTabSelected changed)

public TabListener(Fragment fragment, String tag) {
  this.fragment = fragment;
  this.fragmentTag = tag;
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {

  ft.replace(R.id.fragment_container, fragment, fragmentTag);
}  

With this I only need to add a string (as tag) when adding the TabListener to my fragments and don't have to do further changes.

bish
  • 3,381
  • 9
  • 48
  • 69