I have a floating button in one of my fragments.On click of which an activity pops up.Now the problem is when I go back from the activity to the fragment the fragment does not auto refresh.Also I want it to refresh on back pressed. Within the fragment I have a refresh button in the action bar on pressed of which I call a refreshFragment() method.Is there a way in android that I can call a method from a fragment from within an activity. This is my refreshFragment() method code.
public void refreshFragment()
{
Fragment fragment = new BillingFragment();
android.support.v4.app.FragmentManager fragmentMg = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTrans = fragmentMg.beginTransaction();
fragmentTrans.replace(R.id.container_body, fragment, "TABBILLING");
fragmentTrans.addToBackStack(null);
fragmentTrans.commit();
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Billing");
//adapter.notifyDataSetChanged();
}
And I call it inside my on create view method as follows :-
((MainActivity)getActivity()).setFragmentRefreshListener(new MainActivity.FragmentRefreshListener()
{
@Override
public void onRefresh() {
refreshFragment();
}
});
I even tried calling displayView() method of my main activity from another activity by creating an object of MainActivity as follows:-
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
MainActivity main = new MainActivity();
main.displayView(1);
count = 0;
return true;
But it gave me a null pointer exception.This is my displayView() method.
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
public void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new HomeFragment();
title = getString(R.string.title_home);
break;
case 1:
fragment = new BillingFragment();
title =getString(R.string.title_billing);
break;
case 2:
fragment = new StockViewFragment();
title =getString(R.string.title_stockview);
break;
case 3:
fragment = new BhishiManagementFragment();
title= getString(R.string.title_bhishiview);
break;
case 4:
fragment = new ReportingFragment();
title=getString(R.string.title_reporting);
break;
case 5:
fragment = new VendorManagementFragment();
title=getString(R.string.title_vendormanagement);
break;
case 6:
fragment = new CustomerMgmt();
title = getString(R.string.title_custmgmt);
break;
default:
break;
}
Any help is appreciated.Thank you :)
I assume you are trying to refresh fragment when coming back from an activity (that activity does not hosting the fragment which needs to be refreshed), if my assumption is correct please try the below approach, incase not please clarify the question with more info.
Try : Have a boolean variable in fragment and update its value true or false based on fragment visible state using its lifecycle methods.
public class SampleFragment extends Fragment {
private boolean shouldRefreshOnResume = false;
public static SampleFragment newInstance() {
SampleFragment fragment = new SampleFragment();
return fragment;
}
public SampleFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_blank,
container, false);
}
@Override
public void onResume() {
super.onResume();
// Check should we need to refresh the fragment
if(shouldRefreshOnResume){
// refresh fragment
}
}
@Override
public void onStop() {
super.onStop();
shouldRefreshOnResume = true;
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments