Search code examples
androidandroid-fragmentsandroid-tabs

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


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.


Solution

  • 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();
        }
    
    }