0
我想在運行時創建事物,並且在我想要設置的FragmentActivity中使用全局變量,然後由onActivityCreated()方法中的Fragments使用它們。但是顯然它們在創建時是空的,直到創建Fragment之後才需要使用這些數據。有沒有人制定過這樣做的好方法?我目前使用ActionBarSherlock來做到這一點。如何在使用ActionBarSherlock創建Activity之後刷新Fragment
我希望能夠刷新片段,但不是活動,因爲一切都會再次爲空。
請任何人都可以幫助我。先謝謝你。
FragmentActivity代碼:
public class SlidingTabsActivity extends SherlockFragmentActivity
{
/* Sliding tabs */
private ViewPager viewPager;
private TabsAdapter tabsAdapter;
private ActionBar actionBarTabs;
/* Custom tab view */
private LayoutInflater customTabViewLayoutInflater;
private View customTabView;
/* Layer interfaces */
private CommsLayerInterface commsLayerInterface;
private DeviceLayerInterface deviceLayerInterface;
private PopupFirmware popupFirmware; // Popup firmware class instance
/* Data set information */
private DeviceInfo deviceInfo;
private Pages pages;
private Page page1;
private Page page2;
private Page page3;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
viewPager = new ViewPager(this);
viewPager.setId(R.id.pager);
setContentView(viewPager);
tabsAdapter = new TabsAdapter(this, viewPager); // Declares the tabs adapter class with the view pager view
popupFirmware = new PopupFirmware(this); // Declaring popup firmware class
commsLayerInterface = new CommsLayerInterface();
deviceLayerInterface = new DeviceLayerInterface(this);
deviceLayerInterface.setCommsLayerInterface(commsLayerInterface);
/* Custom tab view instances */
customTabViewLayoutInflater = (LayoutInflater) this.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
customTabView = customTabViewLayoutInflater.inflate(R.layout.tabs_spinner, null);
/* Custom view functions */
TabCustomViewFunctions.createSpinnerList(this, customTabView, deviceLayerInterface);
TabCustomViewFunctions.createNewFile(this, customTabView, deviceLayerInterface);
TabCustomViewFunctions.saveFile(customTabView, deviceLayerInterface);
/* Set up pages and device info */
deviceInfo = deviceLayerInterface.getDeviceInfo();
pages = deviceLayerInterface.getPages();
setUpPages(pages);
/* Action Bar */
actionBarTabs = getSupportActionBar();
actionBarTabs.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBarTabs.setCustomView(customTabView);
actionBarTabs.setDisplayShowCustomEnabled(true);
/* Adds fragments to the tabs adapter */
tabsAdapter.addTab(actionBarTabs.newTab().setText("FRAG1"), Fragment_1.class, null);
tabsAdapter.addTab(actionBarTabs.newTab().setText("FRAG2"), Fragment_2.class, null);
tabsAdapter.addTab(actionBarTabs.newTab().setText("FRAG3"), Fragment_3.class, null);
}
}
片段代碼:
public class Fragment_1 extends SherlockFragment
{
private View view;
private CustomLayout layout; // Container view layout class instance
private SlidingTabsActivity slidingTabsActivity;
private DeviceInfo deviceInfo;
private Page page;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
view = inflater.inflate(R.layout.fragment_custom, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
/* Creates the new views from the layout class */
slidingTabsActivity = (SlidingTabsActivity) getSherlockActivity();
deviceInfo = slidingTabsActivity.getDeviceInfo();
page = slidingTabsActivity.getPage1();
if(page != null & deviceInfo != null)
{
layout = new CustomLayout(slidingTabsActivity);
layout.setDeviceInfo(deviceInfo);
layout.setPage(page);
layout.createView();
System.out.println("Page and device info are NOT null!");
}
else
{
System.out.println("Page and device info are null!");
}
}
}