android get value from all fragment tab

2019-03-21 18:28发布

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条回答
你好瞎i
2楼-- · 2019-03-21 19:23

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

查看更多
登录 后发表回答