0

When my MainFragment first loads, it checks for internet connectivity. If there is internet, it loads the content from online. However, if there is no internet connectivity, I replace the existing fragment MainFragment within the container main_browse_fragment with InternetConnectivityFragment.

Within InternetConnectivtyFragment, I have a Retry button that re-checks the internet, and if there is internet connectivity, I remove InternetConnectivityFragment and call popbackstack.

The problem is, when popbackstack is called, I dont know how to reload the online data from within MainFragment. The online data loading and internet check is done within onActivityCreated of MainFragment.java, but when popbackstack is called within InternetConnectivityFragment.java, the view of MainFragment is blank

Obviously this is because the fragment is not "created" and onActivityCreated is not called again, so no data is reloaded.

Here's the relevant code:

MainFragment.java:

public class MainFragment extends DetailsFragment
{
    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        setupUIElements();

        if (isConnectingToInternet() == true)
        {
            // Call methods to load online data
        }
        else
        {
            InternetConnectivityFragment internetError = new InternetConnectivityFragment();

            getFragmentManager().beginTransaction().add(R.id.main_browse_fragment, internetError).commit();
        }
    }

    ...
}

InternetConnectivityFragment.java:

public class InternetConnectivityFragment extends ErrorFragment
{
    private static final String TAG = "InternetFragment";
    private static final boolean TRANSLUCENT = true;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setErrorContent();
    }

    private void setErrorContent()
    {
        setImageDrawable(getResources().getDrawable(R.drawable.lb_ic_sad_cloud, null));
        setMessage(getResources().getString(R.string.no_internet_message));
        setDefaultBackground(TRANSLUCENT);

        setButtonText(getResources().getString(R.string.retry_connection));

        setButtonClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {

                if (Singleton.getInstance().isConnectedToInternet()  == true)
                {
                    getFragmentManager().beginTransaction().remove(InternetConnectivityFragment.this).commit();
                    getFragmentManager().popBackStack();

                    // How to call methods to re-load online data from MainFragment????
                }
            }
        });
    }
}

I have tried to perform the following within the onClick method:

MainFragment mainFragment = new MainFragment();

getFragmentManager().beginTransaction().remove(InternetConnectivityFragment.this).commit();
getFragmentManager().popBackStack();
getFragmentManager().beginTransaction().add(R.id.main_browse_fragment, mainFragment).commit();

However, I believe this is wrong as it just adds a new fragment MainFragment on top of an already existing fragment.

Can someone point me in the right direction?

Thanks

Pangu
  • 3,721
  • 11
  • 53
  • 120
  • Can you replace the fragment instead of just popping it? – Eenvincible Jun 26 '16 at 20:56
  • I tried `getFragmentManager().beginTransaction().replace(R.id.main_browse_fragment, mainFragment).commit()` and commented out `popBackStack`, but that still means having to create a new `MainFragment`, which I'm sure is not the correct way. – Pangu Jun 26 '16 at 21:00
  • 1
    I have been thinking of another approach to this: you can use just one fragment instead of two; now, when there is no content because of network, set visibility of a button and text to visible; when net is visible, set them to gone – Eenvincible Jun 26 '16 at 21:05
  • I could use that approach, but then again, I'm using the `ErrorFragment` class, which has pretty much everything set up for me in terms of displaying the internet error content and button, which is why I'm using 2 fragments. – Pangu Jun 26 '16 at 21:28
  • I found valid solutions in these SO posts - [Retrieve a Fragment from a ViewPager](http://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager) and in [Android, How to restart/refresh a fragment from FragmentActivty](http://stackoverflow.com/questions/11578000/android-how-to-restart-refresh-a-fragment-from-fragmentactivty) which you can look into. Hope that helps. Happy coding. :) – Teyam Jul 02 '16 at 16:16

0 Answers0