0

My layout look like this:

enter image description here

my fragmentActivity use ViewPager + TabHost

there are 3 tabs in tab label

the button "save" is in my fragmentActivity

I hope I can save data in all 3 tabs by calling their save() method when I click save button

but I don't know how to call them

is there any way to call method from child fragments?

here is my code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.actreg);     
    this.initialiseTabHost(savedInstanceState);



    intialiseViewPager();
    save=(Button)findViewById(R.id.save);
   save.setOnClickListener(new OnClickListener() {
    .....
        }

    }
});
}

private void initialiseTabHost(Bundle args) {



     tabHost = (TabHost)findViewById(android.R.id.tabhost);
     tabHost.setup();
    TabInfo tabInfo = null;
     FragmentActivity.AddTab(this, this.tabHost, this.tabHost.newTabSpec("Tab1").setIndicator("Tab1"),
            (tabInfo = new TabInfo("Tab1", fragment1.class, args)));
    this.myHashMapTabInfo.put(tabInfo.tag, tabInfo);
    ......



    tabHost.setOnTabChangedListener(new OnTabChangeListener() {.....
        }
    });
}
@Override

private static void AddTab(fragmentActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
    tabSpec.setContent(activity.new TabFactory(activity));
    tabHost.addTab(tabSpec);
}
private class TabInfo {
    private String tag;
    private Class<?> clss;
    private Bundle args;
    private Fragment fragment;

    TabInfo(String tag, Class<?> clazz, Bundle args) {
        this.tag = tag;
        this.clss = clazz;
        this.args = args;
    }
}
class TabFactory implements TabContentFactory {

    private final Context mContext;

    public TabFactory(Context context) {
        mContext = context;
    }

    public View createTabContent(String tag) {
        View v = new View(mContext);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }
}
范植勝
  • 787
  • 2
  • 6
  • 14
  • 1
    there is an alternative i know, you can send bundle to each fragment from your fragment activity. – Nitin Misra Dec 02 '13 at 07:26
  • I don't understand what your problem is. When the Activity creates the Fragments, it will have a reference to each one. All you have to do is make sure the Fragments `Save()` method is public and the Activity simply does something like the following... `myFragment.save()` – Squonk Dec 02 '13 at 07:33
  • but I really don't know how to get the reference,here is my code: – 范植勝 Dec 02 '13 at 07:56

4 Answers4

0

Just put the data in global variables, define it in your Activity.

Techfist
  • 4,314
  • 6
  • 22
  • 32
0

For future Googlers- Call an activity method from a fragment

From fragment to activty:

((YourActivityClassName)getActivity()).yourPublicMethod();

From activity to fragment:

FragmentManager fm = getSupportFragmentManager();

//if you added fragment via layout xml
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
fragment.yourPublicMethod();
If you added fragment via code and used a tag string when you added your fragment, use findFragmentByTag instead:

YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag");
Community
  • 1
  • 1
Chris Fremgen
  • 4,649
  • 1
  • 26
  • 26
0

create a public method that contains the setOnClickListener of the button. This method, you can call from the Fragment to this way:

((YourActivityClassName)getActivity()).yourPublicMethod();
Xinoon
  • 85
  • 1
  • 3
-1

You have saved all data in myHashMapTabInfo. so you just need to write a method to save myHashMapTabInfo in save button.

save.setOnClickListener(new OnClickListener() {
    saveInfo(myHashMapTabInfo);
};
yushulx
  • 11,695
  • 8
  • 37
  • 64