How to refresh a fragment from an activity in android?

AndroidNewBee

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 :)

Guru_VL

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to refresh the fragment after activity finished in android?

From Dev

Android refresh a fragment list from its parent activity

From Dev

Android: refresh/update fragment from another activity on button clicked

From Dev

Android: refresh/update fragment from another activity on button clicked

From Dev

how to make the Fragment Refresh itself, from another activity

From Dev

How to send cursor from android activity to fragment

From Dev

How to update Android fragment from activity?

From Dev

How to pass value from activity to fragment in android

From Dev

android from activity to fragment

From Dev

How to refresh a Fragment in android

From Dev

Android - how to update activity view from fragment contained in the activity

From Dev

How to pass a String from an activity to fragment of a tabbed activity in android

From Dev

refresh a fragment from its parent activity

From Dev

refresh a fragment from its parent activity

From Dev

Refresh fragment of ViewPager from parent activity

From Dev

How to refresh contents of Fragment through its Activity?

From Dev

Call activity from fragment in android

From Dev

Call activity from fragment in android

From Dev

How to refresh fragment B from fragment A?

From Dev

How to create an interface to get info from a Fragment to an Android Activity?

From Dev

Android - How to access a getter method in parent Activity from Fragment

From Dev

Android - How do I "start" or "initialize" a Fragment from an activity?

From Dev

Android: How to call Fragment class from activity using Intent

From Dev

Android How to pass Object User to fragment from activity

From Dev

Android How to call Fragment from my Main Activity

From Dev

Android - How to send data from main activity to loaded fragment

From Dev

How to send data from an activity to a Fragment in Android using interfaces?

From Dev

Android; How to return a method for value from an activity inside a fragment?

From Dev

How to intent from activity to fragment picture ( android studio)

Related Related

  1. 1

    How to refresh the fragment after activity finished in android?

  2. 2

    Android refresh a fragment list from its parent activity

  3. 3

    Android: refresh/update fragment from another activity on button clicked

  4. 4

    Android: refresh/update fragment from another activity on button clicked

  5. 5

    how to make the Fragment Refresh itself, from another activity

  6. 6

    How to send cursor from android activity to fragment

  7. 7

    How to update Android fragment from activity?

  8. 8

    How to pass value from activity to fragment in android

  9. 9

    android from activity to fragment

  10. 10

    How to refresh a Fragment in android

  11. 11

    Android - how to update activity view from fragment contained in the activity

  12. 12

    How to pass a String from an activity to fragment of a tabbed activity in android

  13. 13

    refresh a fragment from its parent activity

  14. 14

    refresh a fragment from its parent activity

  15. 15

    Refresh fragment of ViewPager from parent activity

  16. 16

    How to refresh contents of Fragment through its Activity?

  17. 17

    Call activity from fragment in android

  18. 18

    Call activity from fragment in android

  19. 19

    How to refresh fragment B from fragment A?

  20. 20

    How to create an interface to get info from a Fragment to an Android Activity?

  21. 21

    Android - How to access a getter method in parent Activity from Fragment

  22. 22

    Android - How do I "start" or "initialize" a Fragment from an activity?

  23. 23

    Android: How to call Fragment class from activity using Intent

  24. 24

    Android How to pass Object User to fragment from activity

  25. 25

    Android How to call Fragment from my Main Activity

  26. 26

    Android - How to send data from main activity to loaded fragment

  27. 27

    How to send data from an activity to a Fragment in Android using interfaces?

  28. 28

    Android; How to return a method for value from an activity inside a fragment?

  29. 29

    How to intent from activity to fragment picture ( android studio)

HotTag

Archive