Suppose we have an application for viewing notes using fragments and RecyclerView . Notes are stored in the database.

If we write the fragment code as follows:

 private DataBase dataBase; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_notes, container, false); MainActivity activity = (MainActivity)getActivity(); dataBase = new DataBase(activity); activity.changeToolbarTitle(getText(R.string.notes_tabItem).toString()); getNotes(activity); return view; } private void getNotes(MainActivity activity){ RecyclerView recyclerView = (RecyclerView) activity.findViewById(R.id.recycleView); recyclerView.setLayoutManager(new LinearLayoutManager(activity)); recyclerView.setAdapter(new NotesAdapter(dataBase.getNotes())); } 

then after adding a new entry to another Activity and returning to MainActivity , where notes are displayed as RecyclerView , there will be no new note until you restart the application. It is logical: after returning to MainActivity , where the fragment is located, onCreateView() not called, therefore, the call to the getNotes(activity) method, which receives the latest information from the database, also does not.

However, if we consider that after onCreate() always called onResume() , then why not put the call to getNotes(activity) in onResume() ?

enter image description here

 private DataBase dataBase; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_notes, container, false); MainActivity activity = (MainActivity)getActivity(); dataBase = new DataBase(activity); activity.changeToolbarTitle(getText(R.string.notes_tabItem).toString()); return view; } @Override public void onResume() { super.onResume(); getNotes(activity); } 

We still need one instance of activity in onCreateView for other purposes; we create it. But we also need it in onResume() to call getNotes() , and getNotes itself, to use findViewByID and create a new LinearLayoutManager .

Question: can you initialize MainActivity only once or will you have to do it again in onResume ? (As for getNotes , I passed the activity through the parameter, but this is probably not the best solution).

    2 answers 2

    pavel163 gave quite a reasonable solution to the problem. As for startActivityForResult, you can read in detail here, an approximate scheme of how it works is also there (with the only difference that you will not have to be activated in the frigment):

    http://startandroid.ru/ru/uroki/vse-uroki-spiskom/68-urok-29-vyzyvaem-activity-i-poluchaem-rezultat-metod-startactivityforresult.html

    http://startandroid.ru/ru/uroki/vse-uroki-spiskom/69-urok-30-podrobnee-pro-onactivityresult-zachem-nuzhny-requestcode-i-resultcode.html

    That is, you call another Activity not through startActivity, but through startActivityForResult and in the fragment, the code of which is given at the beginning of the question, override the onActivityResult method and call in it your own method

     MainActivity activity = (MainActivity)getActivity(); getNotes(activity); 

      Make the transition to a new activity using the startActivityForResult method. On a new activity send when adding data to the database, you can add to the intent all id records you added, and when you exit this activity, transfer to MainActivity . And in the onActivityResult method onActivityResult get these records by id from the database and add them to the list, while updating RecyclerView either by adding positions, or the entire list

      • Thank you for your reply! Unfortunately, I didn’t understand what exactly should be done when returning to the first activity in the onActivityResult() method. How to update RecyclerView ? If the list is complete, you need to call getNotes() , but as an argument there should be something that will replace the view in the line RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycleView); . - Bokov Gleb