Question

i use this tutorial to make sliding menu this

// this my fragment name

 HomeFragment.java
    package info.androidhive.slidingmenu;

    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class HomeFragment extends Fragment {

        public HomeFragment(){}

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_home, container, false);

            return rootView;
        }

           public void refresh_data(){
        // my code goes here
            }
           }

i want call refresh_data() method from main activity in main menu

 @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // toggle nav drawer on selecting action bar app icon/title
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            // Handle action bar actions click
            switch (item.getItemId()) {
            case R.id.refresh:
            //i should call refresh method here
                return true;
            default:
                return super.onOptionsItemSelected(item);
            }
    }

can any one help me , how can i assign fragment tag here thank you in advance

Was it helpful?

Solution

set setHasOptionsMenu(true); on create view

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_messages, container,
                false);
                     //your code here

                  setHasOptionsMenu(true);
 }

and override on option item selected

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
       // consider your menu have refresh item
    case R.id.refresh:
        Toast.makeText(getActivity(), "Refresh active", Toast.LENGTH_LONG).show();
        refresh_messages();
        break;

    default:
        break;
    }
        return super.onOptionsItemSelected(item);
    }

refresh_messages(){


//refresh code here
}

OTHER TIPS

In your main activity, assuming your fragment has an ID called R.id.details,

HomeFragment f = (HomeFragment)getFragmentManager().findFragmentById(R.id.details);
if(f != null) {
  f.refresh_data();
}

You can assign an ID to a fragment in your XML layout file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top