android get value from all fragment tab

你说的曾经没有我的故事 提交于 2020-01-01 03:37:15

问题


i had declare framgmentActivity like below:

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
    mTabHost.addTab(mTabHost.newTabSpec("basic").setIndicator("Basic",getResources().getDrawable(R.drawable.ic_launcher)),BasicProductFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("taxes").setIndicator("Taxes",getResources().getDrawable(R.drawable.ic_launcher)),TaxesProductFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("price").setIndicator("Price",getResources().getDrawable(R.drawable.ic_launcher)),PriceProductFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("stock").setIndicator("Stock",getResources().getDrawable(R.drawable.ic_launcher)),StockProductFragment.class, null);

Now in all frament i had declare two or three edittext now in this activity, i want to get data from that all edittext from diffrent frangment tab.


回答1:


A singleton class could help solve your problem.

public class GlobalApp {
    private static GlobalApp instance = new GlobalApp();

    private GlobalApp() {}

    public static GlobalApp getInstance() {
        return instance;
    }

    public Details details = new Details ();

}

Then use in your Fragment class like this..

GlobalApp.getInstance().details.setSomeData("something");

Now you can get all the values which are changed in those fragment in your mainActivity

 GlobalApp.getInstance().details.getSomeData();

I have given the same answer for another question which has some relation to this.

Communicative Between Fragments on Android



来源:https://stackoverflow.com/questions/17191973/android-get-value-from-all-fragment-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!