Khurram Majeed
Khurram Majeed

Reputation: 2391

Interaction between Activity and Fragments

In my application the main activity hosts two fragments and attached to acticity as ActionBarTabs. using the following code. NOTE: activity and the 2 fragments are defined in seperate xml layout files (See the picture at the bottom)

    private void createActionTabs() {
        ActionBar actionBar = getActionBar();

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        //Status Tab
        String strStatus = getResources().getString(R.string.lblStatus);
        ActionBar.Tab statusTab = actionBar.newTab();

        statusTab.setText(strStatus);
        TabListener<SFrag> sFTabListener = new TabListener<SFrag>(this, strStatus, SFrag.class);
        statusTab.setTabListener(sFTabListener);
        actionBar.addTab(statusTab);

        //Controller Tab attached the same way
        .....

}

The ActionBar Items (start and refresh) are added using

    public boolean onCreateOptionsMenu(Menu menu) {
       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.main_menu, menu);
       return true;
}

Now coming to my problem:

  1. I want to update the data shown on the Status Fragment at application load, fragment resume and click of refresh menu item from the action bar. Now when I try to access the Status fragment from Main Activity using the following code

    SFrag frag = (SFrag) getFragmentManager().findFragmentById(R.id.st_frag);
    
        Log.d(TAG, "In Activity SFrag is " + (frag == null ? "null" : "not null"));
    
        if (frag != null) {
            //calls the method to update data
            fragment.updateStatusData(statusInformation);
        }
    

The getFragmentManager().findFragmentById methods always returns null. I even tried the onResume method of the activity, the fragment objects is still returned as null. So how do I access the fragment and thus accessa method of that fragment from host acticty.

  1. Secondly, I am trying to use the action_service (its shown as Start button just for this mockup) in action bar as a toggle for satrting or stopping a background service. I can easily update the title/icon for start menu item from onOptionsItemSelected method (I save the current status running/stoppede in shared-preferences). but when I try to accees the menuItem at onStart/onResume of the activity by using

     MenuItem mi = (MenuItem) findViewById (R.id.action_service);
    

it always returns null. So How Can I access action_service menu Item in onResume/onStart to update it.

My Application looks like thisMy application's mockup

Upvotes: 0

Views: 1767

Answers (2)

hardartcore
hardartcore

Reputation: 17037

First of all if you want to declare MenuItem, you should do it in this way :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.main_activity, menu);
    MenuItem mRefresh = menu.findItem(R.id.refresh_menu_item);   
    return true;
}

About updating your Fragment on applications load or Activity's start, just set selected the tab which holds your Fragment and put the code which will load the data in your Fragment on it's onStart() or onActivityCreated() method. You can override onOptionsItemSelected() in your Fragment, so you can update your views not from your Activity which holds your Fragment, but from it's own class.

Edit: Here is an example how you should handle menu from your Fragment :

    public class MyFragment extends Fragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            setHasOptionsMenu(true); // Do not forget this!!!
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
             super.onCreateOptionsMenu(menu, inflater);
               inflater.inflate(R.menu.first_fragment, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
            super.onOptionsItemSelected(item);
            switch (item.getItemId()) {
            case R.id.action_refresh:
                // Do You stuff here   
                break;
            }
            return super.onOptionsItemSelected(item);
    }
}

Using this you can add and use MenuItem's in your Fragment.

Hope this helps!

Upvotes: 2

tyczj
tyczj

Reputation: 73773

I can answer the MenuItem part just not the other part.

for the menu item as per the docs You can safely hold on to menu (and any items created from it), making modifications to it as desired, until the next time onCreateOptionsMenu() is called.

for example

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    actionMenu = menu;
    return true;
}

so basically anytime you want to change one of the items you can do this

MenuItem item = actionMenu.getItem(0);

Upvotes: 1

Related Questions