Run a method after onViewCreated - android

I have a fragment that creates a view, this view uses other classes and methods, but I want after create the view (inflated and initialized) is visible to the user call a method that is going to display the data usage per app.
Is there a way i can run a method after everything is inflated and initialized?
Thanks

The view does not load until the method is over
That is because you are doing some long task in UI thread. Then even you put that method in onResume() or onStart(), your UI will freeze. You should use AsyncTask if you want do some long thread work after UI is inflated.
see
What is the Android UiThread (UI thread)
https://developer.android.com/topic/performance/threads

try running it into onResume()
It is the last called method of the fragment lifecycle

Related

Asynctask lifecycle inside a Fragment

Could anyone explain what does exactly happens (lifecycle of Asynctask) if for example I have and Activity with a Fragment and from this Fragment I execute an Asynctask where on the onPreExecute I start displaying a ProgressDialog and at some point I close the app while the Asynctask is still running?
As far as I've checked the Fragment doesn't call onDetach nor onDestroy and the Asynctask doesn't reach the onPostExecute method or onCancelled
If i am not wrong you are familiar with lifecycle of an AsyncTask. If not, refer
https://developer.android.com/reference/android/os/AsyncTask.html
To answer why onPostExecute method is not called when we exit the app while the progress bar is still running, I would say it is because, AT holds a reference to the Activity/Context which would be destroyed by the time progress bar decides it's job is done(bg task/thread).
Very useful blog on how to handle ATs wrt fragments and activities by Alex Lockwood.
http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html
Have you set setRetainInstance(true) in your fragment, whenever you start asynctask inside a fragment with setRetainInstance as true the task will continue to run in background without interrupt which is not in the case of an activity
Please ensure that AsyncTask.doInBackground() has been completed. Or, what is more possible, your main thread is stuck somewhere as AsyncTask.onPostExecute() must be executed in main thread.
In the scenario you are speaking about, the Fragment methods "onPause()" and "onStop()" will be called (besides of the ones of the Activity which contains the given Fragment).
As those methods are being called, you should react according to what you want to do in the AsyncTask.
If that's not the case, refer to the link given by #stack_ved.
Anyway, if you want to do any kind of load inside a Fragment, I strongly recommend you to use "Loaders" or "AsyncTaskLoader".
https://developer.android.com/reference/android/content/AsyncTaskLoader.html

MVP: how to notify view about changes after de-attach

Let's imagine situation:
User click on 'Login' button and Fragment(View) call Presenter's method doLogin().
Presenter starts some async work and now Boom! app is closed(moved to recent apps)
Presenter survives and async work is still happening.
Async work finished while app was in the background.
User came back to app, but he doesn't see any notification that work is finished as view was de-attached:
if(isViewAttached()) {
getView().setLoaded(workResult);
}
And I want to fix it. The only way that I see is to use Queue<MessageToView> and when View has attached again, execute every "Message".
I think that there is a library that can handle my case. So, is it? Or what pattern can I use?
See github pages FAQ section:
Can the Presenter and its view be out of sync during a screen
orientation change?
Excellent question. Mosby assumes that all interaction from Presenter
with the View happens on android’s main UI thread. Hence the answer is
no that cannot happen since screen orientation changes are executed on
the main UI thread as well. So either is a screen orientation executed
completely (view reattached) or the presenter invokes the views method
after view is reattached since both run on main UI thread or the
presenter invokes the views methods before starting screen orientation
change.
So as long as your Presenter invokes View methods on main UI thread everything works out of the box.
Try using the Fragment's onResume() lifecycle method, and then call something like presenter.updateViews()

Using result of a thread in OnResume()

My Problem: Is it possible to prevent an activity to call OnResume() when it is being created? As I saw after the OnCreate() and onStart() method runs, the next one is the onResume(), although I only want to have it when I resume the activity from the paused state.
Why do I need this: I launch my activity (FragmentActivity, so lets say OnPostResume() ) starting with a thread which takes about 2-3s to be ready getting data from an external database. After the thread is done, I call a method which needs these data and I want to call it everytime that activity gets visible. The thread runs only when the FragmentActivity is created (onCreate()), and I cannot put the method into the onResume() because onResume() would be running way before the thread would finish its task. So it would receive not-ready data.
Anyone has a better idea?
Not sure of the exact application of this but I'll make a suggestion.
If you use an AsyncTask, you can send it off to get the data you need and in the onPostExcecute() method you can call your method that requires the data or update the view as needed. (It runs on the UI thread)
If you happen to already have the data you need in certain scenarios you could also bypass the AsyncTask and directly update the view.
This AsyncTask can be triggered in the onResume() method.
If I'm missing something, please let me know and I can adjust my suggestion.
I didn't understand the purpose of this, but here's a possible solution:
If you only wish to get the even of onResume on states that didn't have the onCreate before, just use a flag.
In the onCreate, set it to true, in the onResume check the flag (and also set it to false). if it was true, it means the onCreate was called before.
I personally would prefer to check if the result available, rather than always executing the getter-code in onResume. If the user somehow resumes your activity before the background thread is finished, you'd have a call on onResume, but don't want to display a result.
Maybe it would be a good idea to calculate/fetch the values in the thread, and let the thread return immediately (and cause the values to get filled in) if the values are already cached somewhere. That way you'd only have one entry point (the thread) for updating your UI instead of two (the thread and the onResume method).

Execute an AsyncTask after Activity is shown

I'm developing an Android 3.1 application.
I want to execute an AsyncTask after activity is shown. I want to show something to user before execute AsyncTask.
I've read that it is not recommend to execute AsyncTask on onCreate().
Where I have to execute AsyncTask on onStart() or onResume()?
I want to left enough time to show activity before execute it.
onCreate(), onStart() and onResume() are lifecycle methods called by the operating system and shouldn't be called directly. You can however override them to have your code executed at these stages of the activities lifecycle:
However, if you want your AsyncTask to start after all of your Views have been inflated and drawn to the screen then you need to put the code in this:
toReturn.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
toReturn.getViewTreeObserver().removeGlobalOnLayoutListener(this);
// asyncTask.execute();
}
});
In the above example toReturn is a view in your onCreate() method. It can be any view you like.
This pulls a ViewTreeObserver from the View and add's a listener to it which will be called when the view has finished being drawn to the screen. It's important you keep the "removeGlobalOnLayoutListener()` line in as this will stop the code firing every time the View is drawn.
Answer is in onResume()
I hade same requirement in my activity where i need to show some list with other buttons and images..
List were getting data from server so used AsyncTask for that..
But before that required to show empty listview and other part of the screen..
so first when it goes to onCreate() I set empty arraylist to listview's adapter then in onResume() call the Asynctask and in that task fill the ArrayList and call adapter.notifyDataSetChanged()
Then another problem occure..when i go to next activity and come back it always call the asynctask even if i dont require..
So had put some condition like if(arrayList.size()==0) then call asynctask else dont.
You can put yur code in the onWindowsFocusChanged method. You can use a thread inside it to manage the timer to start your specific asynctask.
Be aware that this would be performed each time your activity have the focus, not only the first time you launch your activity (I don't know if this could be a problem for you).
implement a View object and override the onDraw().
that way you'll know exactly when the first screen is visible to the user

Android setContentView operation

I've read that it's important to call setContentView() early in an activity since it builds the view objects that may be manipulated by subsequent code in onCreate().
In terms of lifecycle, does the view get drawn to screen as soon as setContentView() is called, or does it allow the onCreate() function to build/populate the information in the view objects, and wait to actually draw it after onCreate() completes?
Thanks!
does the view get drawn to screen as
soon as setContentView() is called
No.
or does it allow the onCreate()
function to build/populate the
information in the view objects, and
wait to actually draw it after
onCreate() completes?
Yes. The View objects are created immediately as part of setContentView(). However, all drawing operations (from onCreate() or anywhere else) really result in messages being put on a message queue that the main application thread works through.

Categories

Resources