Question

I am trying to refresh the fragment depending on what drawer the user is on...

This was my attempt which does not do anything but does not give me an error.

Here is my code

public void fragmentRefresh() {
        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getFragmentManager();

            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragmentManager.findFragmentByTag("TagForRefresh"))
                    .commit();


    }

Here is the drawer fragment code:

/**
     * Diplaying fragment view for selected nav drawer list item
     */
    private void displayView(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = new HomeFragment();
                break;
            case 1:
                fragment = new BrowseFragment();
                break;
            case 2:
                fragment = new InviteFragment();
                break;
            case 3:
                fragment = new ProfileFragment();
                break;
            case 4:
                fragment = new SettingsFragment();
                break;
            case 5:
                callConfirmLogout();
                break;


            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment, "TagForRefresh").commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }

Implemented in a tab swiper fragment class:

public class ViewVotesActivity extends FragmentActivity implements ActionBar.TabListener{


    //Tab options
    private ViewPager viewPager;
    private TabSwipeAdapter mAdapter;
    private ActionBar actionBar;

    // Tab titles
    private String[] tabs = { "Created", "Up", "Down"};
    //for custom menu to add progress bar to refresh
    private MenuItem menuItem;

    //Search view action bar
    private SearchView search_action_bar;

    //getApplicationContext().getString(R.string.created),getApplicationContext().getString(R.string.upvote), getApplicationContext().getString(R.string.downvote)
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_votes);


        // Initializing tab
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabSwipeAdapter(getSupportFragmentManager(), new UserCreatedFragment(), new UserUpVotesFragment(),new UserDownVotesFragment());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });


    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.home, menu);
        setupActions(menu);
        callSearchActionBar(menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar actions click
        switch (item.getItemId()) {
            case R.id.action_refresh:
                menuItem = item;
                menuItem.setActionView(R.layout.refresh_progress_bar);
                //menuItem.expandActionView();
                fragmentRefresh();

                TestTask task = new TestTask();
                task.execute("test");
                return true;
            case R.id.action_discover:
                addNewItem();
                return true;
            case R.id.action_search:
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void addNewItem() {
        // user redirect to register followup
        Intent createItemIntent = new Intent(ViewVotesActivity.this, CreateItemActivity.class);
        // Closing all the Activities
        createItemIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // Add new Flag to start new Activity
        createItemIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // Staring Login Activity
        startActivity(createItemIntent);
    }

    public void fragmentRefresh() {
//        // Insert the fragment by replacing any existing fragment
//        FragmentManager fragmentManager = getFragmentManager();
//
//        fragmentManager.beginTransaction()
//                .replace(R.id.frame_container, fragmentManager.findFragmentByTag("TagForRefresh"))
//                .commit();


    }


    private class TestTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            // Simulate something long running
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            menuItem.collapseActionView();
            menuItem.setActionView(null);
        }
    };

    public  void setupActions(Menu menu) {
        MenuItem refreshItem = menu.findItem(R.id.action_refresh);
        MenuItem discoverItem = menu.findItem(R.id.action_discover);

        if (isAlwaysExpanded()) {

        } else {
            refreshItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
                    | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

            discoverItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
                    | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        }

    }

    public void callSearchActionBar(Menu menu) {
        //find the menu item and set search view at the same time
        MenuItem searchItem = menu.findItem(R.id.action_search);

        search_action_bar
                = (SearchView)  searchItem.getActionView();

        if (isAlwaysExpanded()) {
            search_action_bar.setIconifiedByDefault(false);
        } else {
            searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
                    | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        }

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        if (searchManager != null) {
            List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();

            SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
            for (SearchableInfo inf : searchables) {
                if (inf.getSuggestAuthority() != null
                        && inf.getSuggestAuthority().startsWith("applications")) {
                    info = inf;
                }
            }
            search_action_bar.setSearchableInfo(info);
        }

        search_action_bar.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            public boolean onQueryTextChange(String newText) {
                Log.e("Query = ", newText);
                return false;
            }

            public boolean onQueryTextSubmit(String query) {
                Log.e("Submit", "Submited " + query);
                // user redirect to register followup
                Intent searchIntent = new Intent(ViewVotesActivity.this, SearchActivity.class);

                searchIntent.putExtra("search", query);

                // Closing all the Activities
                searchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                // Add new Flag to start new Activity
                searchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                // Staring Login Activity
                startActivity(searchIntent);
                return false;
            }

            public boolean onClose() {
                Log.e("query close", "query closingggg");
                return false;
            }
        });
    }
    protected boolean isAlwaysExpanded() {
        return false;
    }


}

No correct solution

OTHER TIPS

UPDATE: It looks like you're trying to tell an Activity that it's time to change the content fragment depending upon a selection in a Navigation Drawer.

This problem has two parts.

  1. You need to provide a mechanism to communicate from your Drawer to your Activity, which, will, in turn, proceed to perform the necessary actions.
  2. Your Fragments must either be Detached, Destroyed, Recreated and Reattached (inneficient, why?) or simply implement a mechanism that tells them: Hey, it's time to Refresh your content.

I don't see any reason why you need to completely destroy the Fragment's view if you only need to reset its data. Seems like a design flaw.

Replacing a Fragment should not mean you want to completely destroy it, since the user can simply go back or press the recently removed fragment. It would be inefficient to recreate it again.

Like I mentioned in the comments, more code is needed to see what your current approach is.

Normally a simple Interface (like the one I described below) should suffice. Your activity can receive the "clicks" in the Drawer and decide which fragment to replace (if needed).

Please be a lot more specific and provide more code.


Old Response:

Warning: if you're trying to replace the current fragment, make sure you're not getting the wrong FragmentManager. You do getFragmentManager() but I'm sure you're using the support library and therefore need to do getSupportFragmentManager();. You've failed to mention what version you're targeting so it's hard to know.

On the other hand,

If what you want to do is tell the current visible fragment to refresh… you should use a more common approach to object-to-object communication.

You're over-complicating things.

(warning: pseudo code)

Create an interface like:

public interface Refreshable{
  void onShouldRefresh();
}

Have a DrawerController capable of storing observers/listeners…

public class DrawerController {
    private List<Refreshable> mListeners = new ArrayList<Refreshable>();
    public void addRefreshListener(E listener) {
        if (listener != null && !mListeners.contains(listener)) {
            mListeners.add(listener);
        }
    }
    public void removeRefreshListener(E listener) {
        if (listener != null) {
            mListeners.remove(listener);
        }
    }
}

Make your fragments implement it (and subscribe to the event when they are visible)

public class HomeFragment extends Fragment implements Refreshable {
    // your fragment code
    public void onShouldRefresh(){
         // do the refresh
    }
    @Override
    public void onResume() {
        super.onResume();
        yourDrawerController.addRefreshListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        yourDrawerController.removeRefreshListener(this);
    }
}

Now make a method that will tell the interested parties (in this case your "Refreshable" objects) that it's time to refresh, add this to your DrawerController…

public void refresh(){
    for (Refreshable listener : mListeners) {
        if (listener != null) {
            listener.onShouldRefresh();
        }
    }     
}

Finally, in your code, call refresh() and have a beer.

Did I miss your point? it's not 100% clear to me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top