0

I have 3 Fragments: Frag1, Frag2 and Frag3. When I open My application, Frag1 loads on first page and shows a web page. Frag2 on second page should show a image extracted from the same web page. Frag3 on page 3 should show some text extracted from the same page. In each fragment I was able to extract and put the content. Problem facing with below code is:

  1. When I choose a date from menu options, I am setting a new date string / url string with which all three fragments should refreshed with new data. This is not happening and I am not sure why. Only Frag1 and Frag3 i.e Page 1 and Page 3 are reflecting after couple of seconds during swipe. Page2 i.e where I have a imageView and a button to save that image which is Frag2 is never getting reflected.

Below is the FragmentActivity and Fragment implementation.

Could you please help me how can I refresh all Fragments once I choose a new date from menu option function changeDate() i.e case R.id.date_settings.

Below is the snippet of main functionality. Everything is working fine but the problem mentioned above.

public class MainActivity extends FragmentActivity {

    public int year;
    public int month;
    public int day;
    public String date_string;
    public static String urlString;
    public static String urlImageString;
    ViewPager mViewPager;
    SectionsPagerAdapter mSectionsPagerAdapter;
    FragmentManager fragmentManager;
    TextView pageTitleText;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        createDateString();

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.

            Fragment fragment;
            Bundle args = new Bundle();
            switch(position) {
            case 0:
                fragment = new Frag1();
                args.putInt(FullPageFragment.ARG_SECTION_NUMBER, position + 1);
                fragment.setArguments(args);
                return fragment;
            case 1:
                fragment = new Frag2();
                args.putInt(ImageFragment.ARG_SECTION_NUMBER, position + 1);
                fragment.setArguments(args);
                return fragment;
            case 2:
                fragment = new Frag3();
                args.putInt(MessageFragment.ARG_SECTION_NUMBER, position + 1);
                fragment.setArguments(args);
                return fragment;
            default:
                return null;
            }
        }


        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
        }
    }

    public static class Frag1 extends Fragment {
        public static final String ARG_SECTION_NUMBER = "section_number";
        WebView webView;
        public FullPageFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main,
                    container, false);

            webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );
            webView.loadUrl(urlString);
            return rootView;
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            // TODO Auto-generated method stub
            super.onSaveInstanceState(outState);
        };  
    };


    public static class Frag2 extends Fragment {

        public static final String ARG_SECTION_NUMBER = "section_number";
        ImageLoader imgLoader;  // class that downloads and display image also has save option
        ImageView imgview;
        int loader;

        public ImageFragment() {
        };

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.image_view,
                    container, false);

            loader = R.drawable.ic_launcher;
            imgview = (ImageView) rootView.findViewById(R.id.imageView1);
            imgLoader = new ImageLoader( super.getActivity().getApplicationContext());
            imgLoader.DisplayImage(urlImageString, loader, imgview, file_string);
            }

            Button imgBtn = (Button) rootView.findViewById(R.id.button1);
            imgBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    Log.i(TAG, "onClick: Saving file: < " + file_string + ">");
                    imgLoader.SaveImage(file_string);
                }
            });
            return rootView;
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            // TODO Auto-generated method stub
            super.onSaveInstanceState(outState);
        };

    }

    public static class Frag3 extends Fragment {

        public static final String ARG_SECTION_NUMBER = "section_number";
        WebView webView;
        public String finalUrl;

        public MessageFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.message_view,
                    container, false);
            webView.loadData(finalMsg, "text/html", "UTF-8");
            return rootView;            
        };

        @Override
        public void onSaveInstanceState(Bundle outState) {
            // TODO Auto-generated method stub
            super.onSaveInstanceState(outState);
        }
    };

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.date_settings:
                changeDate();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void changeDate() {
        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dpd = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year1,
                            int monthOfYear, int dayOfMonth) {
                        //txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
                        year = year1;
                        month = monthOfYear;
                        day = dayOfMonth;
                        Log.i(TAG, "onDateSet: date has changes resetting values");
                        createDateString();

                        createUrl();
                        createUrImage();
                        createFileString();

                    }
                }, year, month, day);
        dpd.show();
    };

    public void createUrl() {
        //implementaion is here
    }

    public void createUrImage() {
        //implementaion is here
    }

    public void createFileString() {
        //implementaion is here
    }
}
  • Sorry can't comment because of less reputation. Why do you have two inner classes with the same name Frag1? – Jayesh Elamgodil Sep 17 '14 at 20:09
  • sorry. Its copy mistake to post and not the problem. I have changed it. There are 3 Fragment Implementations Frag1, Frag2 and Frag3. Frag2 is not refreshing as mentioned in the problem list. Even if I interchange like If case 0, call frag3 and case 1 call Frag1 etc.. Fragment which is being showed on 2nd page (i.e case 1) not refreshing. Also during change date from menu option selection. – Sudarshan sridhar Sep 18 '14 at 12:30

0 Answers0