Office Hours — Today, July 23

Thursday, July 21

Jul 23
8:50 AM
Mark M.
has entered the room
Mark M.
turned on guest access
9:10 AM
ndrocchietto_ivano
has entered the room
Mark M.
hello, Ivano!
how can I help you today?
ndrocchietto_ivano
good morning
one sec loading brain...
9:15 AM
ndrocchietto_ivano
yes. i have a MainActivity and I have a Fragment attached. As I query the db by means of some Loaders, I have the need to reload the fragment to refresh the view. I reload the fragment both under the fragment itself both from a different Dialog Fragment.
View paste
the approach to follow I found on SO is to  use this: // Reload current fragmentFragment frg = null;frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();ft.detach(frg);ft.attach(frg);ft.commit();
(sorry is not formatted well)
but I found much more pratical to launch an explicit intent that loads from scratch the main activity. In this way baing loaded the main activity the attached fragment is automatically called in onCreate and onCreateView and i obtain the result I want.
my question is:
it is this an efficient way to proceed or I mess up a bit the memory stacking activity over activity continuously, while instead working directly one fragment per time would be more indicated? I follow the launch MainActivity.class explicit intent the better way to proceed (STOP)
Mark M.
let's back up a few steps
9:20 AM
Mark M.
why do you feel that you need to reload the fragment to refresh the view?
(let alone reload the *activity* to refresh the view)
ndrocchietto_ivano
because i have a list view attached to the fragment with Adapter and loaders, and need to refresh for instance because I need to launch a new query as instead of SELECT *, SELECT column WHERE, for instance.
Mark M.
why not just something like restartLoader()?
ndrocchietto_ivano
did not know about that
Mark M.
I haven't used it, but it's on LoaderManager, and it should allow you to basically force the loader to load again
another alternative would be to destroyLoader() the old one and initLoader() a new one, perhaps with a fresh ID
ndrocchietto_ivano
that is great but i have 6 different LoadInit numbers
Mark M.
so?
ndrocchietto_ivano
well I am thinking that loading a loader again could be detrimental if I do not detach before the fragment, obtaining a new list view on top of the old list view
Mark M.
:: shrug ::
ndrocchietto_ivano
that is why i resolve the problem calling a new main activity
:)
Mark M.
I do not know why it would be detrimental
if anything, I would expect it to be better
it certainly will be faster and less memory-intensive
and since you'
and since you are not destroying and recreating the widgets, it might be easier to keep the user's context (e.g., scroll position in a list)
9:25 AM
ndrocchietto_ivano
true
I catch this on restoresavedinstance anyway
Mark M.
to draw an analogy, what you are doing is like a Web page causing itself to reload from the Web server as a way of updating it, instead of using AJAX and updating the DOM
this works, and in the 1990's, it was pretty much our only option
ndrocchietto_ivano
ah ah
great example indeed
it works this is for sure:)
well Mark, I wish you a nice week off, and thank you very much.
Mark M.
you're welcome!
10:00 AM
Mark M.
that's a wrap for today's chat
the transcript will be posted to https://commonsware.com/office-hours/ shortly
the next chat is Thursday at 9am US Eastern
have a pleasant day!
ndrocchietto_ivano
has left the room
Mark M.
turned off guest access

Thursday, July 21

 

Office Hours

People in this transcript

  • Mark Murphy
  • ndrocchietto_ivano