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
Aucun commentaire:
Enregistrer un commentaire