Search code examples
androidandroid-fragmentsfragmentstatepageradapterpage-fragments

reload PageFragment data after update fields in viewpager


I used this article and I have a question.

In PageFragment (extends Fragment) I have some fields that load from DB (sqlite) and this fragment have a button that after click, update table in db. I want to reload fragment to show updated data (without swiping). I know that android keep states in memory, but I need to show updated fields whithout swiping.

I try to use POSITION_NONE, setOffscreenPageLimit, notifyDataSetChanged, detach, attach and etc..but none of them work and I confused about them.


Solution

  • a more proper way of doing that would be to put the initial update logic in separate method and call the method after updating the db.

    public void onCreateView(){
        //your view creation and inflation
        //call populateUI after inflation here
    }
    
    public void onResume(){
        //or here
    }
    
    //take data from someplace and populate your UI may be from database
    public void populateUI(){
      //get values and set fields 
    }
    
    public void onClick(View v){
        //the button click
        //update db
        //then
        populateUI();
    }