pass value from activity to fragment using bundle in tabbed activity

前端 未结 2 1180
-上瘾入骨i
-上瘾入骨i 2021-01-27 23:47

I am a java-illiterate, and still trying to develop a app for my personal use. I have started with android-studio\'s \"Tabbed-Activity\", and mostly unaltered except a fragment

2条回答
  •  再見小時候
    2021-01-28 00:41

    In your MainActivity, your sunFragment is unused. Remove this part:

    /*Bundle bundle = new Bundle();
    bundle.putDouble("loclat", 25.4358);
    bundle.putDouble("loclang",81.8463);
    Fragment SunFragment = new SunFragment();
    SunFragment.setArguments(bundle);*/
    

    You have to set bundle to fragment inside your SectionsPagerAdapter

    case 0:
        Bundle bundle = new Bundle();
        bundle.putDouble("loclat", 25.4358);
        bundle.putDouble("loclang",81.8463);
    
        Fragment sunFragment = new SunFragment();
        sunFragment.setArguments(bundle);
    
        return sunFragment;
    

    But if you need to set the bundle to fragment from MainActivity. Then use a callback in that purpose.

提交回复
热议问题