How to call onCreateView method or update fragment?

I would like to update or call onCreateView in the following code. I have contex menù to delete an element, and after I want to update the fragment with a new element. Thanks at all!

public class ItemDetailFragmentBlackBoard extends fragment {

@Override

 public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle  savedInstanceState) {
        ....
       return rootView;
 }

}

 /** Menu on LongClick */
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
    {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Context Menu");
    menu.add(0, v.getId(), 0, "Delete");
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    if(item.getTitle()=="Delete"){
        String status="";
        AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
        int posizione = info.position;
        String[] messaggioDaCancellare= S.getMessaggiInfo().get(posizione); 
        try{
            JSONObject del =ProxyUtils.proxyCall("deleteMessage",messaggioDaCancellare[4]);
            status=del.getString("status");
        } catch (Exception e) {
            Log.i("Eccezione", e.toString());
        }
        Activity activity= getActivity();
        if(status.equals("OK")){

                   **HERE......I would like to refresh my fragment o recall onCreateView method...**

            Toast.makeText(activity, "Delete avvenuta", Toast.LENGTH_SHORT).show();
        }else
            Toast.makeText(activity, "Delete non riuscita", Toast.LENGTH_SHORT).show();
    } else {return false;}  
    return true;  
}
+5
source share
3 answers

It is better to define viewGroup as a simple linearLayout (call its screen) in the onCreateView function and populate it with a function like init (). Each time you want to recreate your view, simply remove all descendants of linearLayout and call init ().

You did a great job, but it's more expensive than my simple solution.

+4
source

, fragmet

:

{

    arguments.putString(ItemDetailFragmentBlackBoard.ARG_ITEM_ID, id);
    ItemDetailFragmentBlackBoard fragment= new ItemDetailFragmentBlackBoard();
    fragment.setArguments(arguments);
    getFragmentManager().beginTransaction().replace(R.id.item_detail_container,   fragment).commit();   
}

ItemDetailFragmentBlackBoard - . View, , , , !

+2
public class ItemDetailFragmentBlackBoard extends Fragment {
    public static View _rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (_rootView == null || _isRefreshDashboard) {
        _rootView = inflater.inflate(R.layout.ItemDetailFragmentBlackBoard, container, false);

        // your code can't be change without refreshFragment in here..

        _isDashboardRefresh = false;
    }
}

// Global variables

public abstract class CommonBase extends AppCompatActivity {
    public static boolean _isRefreshDashboard;
}

// Update fragment

CommonBase._isRefreshDashboard = true;
0
source

All Articles