简体   繁体   中英

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.

The traditional way is to declare a callback interface in your Activity and let your Fragment implement it, then connect them together. Here is the guidelines: https://developer.android.com/guide/components/fragments.html#EventCallbacks .

The better way is to use something like Message Bus to remove strong dependencies between your Fragment and Activity. There are plenty of articles saying about this.

If you want to structure your application better and get rid of the headache in UI stuffs' communication, I recommend you to adopt a Model-View-Presenter framework. Here is an example: http://robo-creative.github.io/mvp .

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM