How to call a method exist in ActionBar Tab Fragment from main activity

Riskhan

I have an application which uses ActionBar Tab Fragment for categorizing data in different tabs. I need to call a method from Tab Fragment when i presses a button from main activity( tab activity). I tried below code

Camera Details Activity

public class CameraDetails extends Activity {
ActionBar.Tab networkTab, userTab;
Fragment networkFragmentTab = new NetworkFragmentTab();
Fragment userFragmentTab = new UserFragmentTab();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cameradetails);

    // Asking for the default ActionBar element that our platform supports.
    ActionBar actionBar = getActionBar();

    // Screen handling while hiding ActionBar icon.
    actionBar.setDisplayShowHomeEnabled(false);

    // Screen handling while hiding Actionbar title.
    actionBar.setDisplayShowTitleEnabled(false);

    // Creating ActionBar tabs.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Setting custom tab icons.
    networkTab = actionBar.newTab().setText("Network"); //.setIcon(R.drawable.bmw_logo);
    userTab = actionBar.newTab().setText("User Account");

    // Setting tab listeners.
    networkTab.setTabListener(new TabListener(networkFragmentTab));
    userTab.setTabListener(new TabListener(userFragmentTab));

    // Adding tabs to the ActionBar.
    actionBar.addTab(networkTab);
    actionBar.addTab(userTab);

    actionBar.setSelectedNavigationItem(1);
    }

public void onClick(View v){
  // call method validate from NetworkFragment like networkfragment.validate();
    }
}
}   

NetworkFragment

public class NetworkFragmentTab extends Fragment {
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.network_layout, container, false);
    return rootView;
}

public boolean Validate(){
    EditText etIpAddress = (EditText)rootView.findViewById(R.id.cd_ip_address);
    Toast.makeText(getActivity(), etIpAddress.getText().toString(), Toast.LENGTH_LONG).show();
    return true;
}
 }

I like to call method validate() of NetworkFragmentTab from onClick.

naran z

Try below code for callback via Interface. You have to create one Interface and that Interface will have one method which will be used to call the method of Fragment from your Activity. Try below code approach, it will solve your problem.

class MyActivity extends Activity {
    private MyInterface mInterface;

    public interface MyInterface {
        void theMethodOfInterface();
    }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cameradetails);
    ...
  }

  public void onClick(View v) {
    mInterface.theMethodOfInterface();
  }

  public void setMyListener(MyInterface listner) {
    this.mInterface = listener;
  }
}

The Fragment is as below:

class MyFragment extends Fragment implements MyInterface {
    ...

      @Override
  public void onCreateView(Bundle savedInstanceState) {
    ...// your code
    ((MyActivity)getActivity()).setMyListener(this);
    ...// your code
  }

    public void someMethodOfFragment() {
        ...
        // your code for method of fragment here
    }

    @Override
    public void theMethodOfInterface() {
        someMethodOfFragment();
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call method from Main Activity into Fragment Adapter

From Dev

How to call method inside the fragment from activity?

From Dev

How call method from Fragment in Activity?

From Dev

How to call ViewHolder method from Activity/Fragment

From Dev

How to load the fragment activity oncreate() method of the actionbar selected tab only if the tab is selected?

From Java

Call an activity method from a fragment

From Dev

Call Fragment method from Activity

From Dev

Call Fragment method from Activity

From Dev

Android How to call Fragment from my Main Activity

From Dev

how to call a fragment method in an activity

From Dev

Call method in fragment from another fragment/activity

From Dev

Call method in fragment from another fragment/activity

From Dev

How can i call a method in my fragment from my activity?

From Dev

how to call a fragment from activity

From Dev

How to call a method from a different class inside the main activity?

From Dev

How to call the method in Main_Activity Class from Helper class?

From Dev

Call dynamic fragment method from activity

From Dev

Call Tabbed Fragment method from Activity

From Dev

Call fragment method from activity but view is null

From Dev

Call a method in a drawer fragment from the base activity

From Dev

Call fragment method from activity but view is null

From Dev

Start a fragment from actionbar tab

From Dev

Call fragment method in an activity?

From Dev

How to fire event from fragment in main activity?

From Dev

How to change data of Main Activity from Fragment

From Dev

Call method in child fragment in parent fragment from activity

From Dev

Call method in child fragment in parent fragment from activity

From Dev

Call a function defined in a fragment, from the main activity that required a view

From Dev

How to call a fragment from another activity

Related Related

  1. 1

    Call method from Main Activity into Fragment Adapter

  2. 2

    How to call method inside the fragment from activity?

  3. 3

    How call method from Fragment in Activity?

  4. 4

    How to call ViewHolder method from Activity/Fragment

  5. 5

    How to load the fragment activity oncreate() method of the actionbar selected tab only if the tab is selected?

  6. 6

    Call an activity method from a fragment

  7. 7

    Call Fragment method from Activity

  8. 8

    Call Fragment method from Activity

  9. 9

    Android How to call Fragment from my Main Activity

  10. 10

    how to call a fragment method in an activity

  11. 11

    Call method in fragment from another fragment/activity

  12. 12

    Call method in fragment from another fragment/activity

  13. 13

    How can i call a method in my fragment from my activity?

  14. 14

    how to call a fragment from activity

  15. 15

    How to call a method from a different class inside the main activity?

  16. 16

    How to call the method in Main_Activity Class from Helper class?

  17. 17

    Call dynamic fragment method from activity

  18. 18

    Call Tabbed Fragment method from Activity

  19. 19

    Call fragment method from activity but view is null

  20. 20

    Call a method in a drawer fragment from the base activity

  21. 21

    Call fragment method from activity but view is null

  22. 22

    Start a fragment from actionbar tab

  23. 23

    Call fragment method in an activity?

  24. 24

    How to fire event from fragment in main activity?

  25. 25

    How to change data of Main Activity from Fragment

  26. 26

    Call method in child fragment in parent fragment from activity

  27. 27

    Call method in child fragment in parent fragment from activity

  28. 28

    Call a function defined in a fragment, from the main activity that required a view

  29. 29

    How to call a fragment from another activity

HotTag

Archive