Only one Looper may be created per thread - android

I have an app which request data information on internet (client-server app), but this communication is very slow, thus i have decided to create an AsyncTask to manage the delay.
inside of doInBackground i call Looper.prepare() then a my "view generator (which retrives data)".
in detail (the problem):
I have an activity that dinamically create the rows of a list view. but every time i try to inflate rows, android throws a Looper exception "Only one Looper may be created per thread"
i followed the steps:
call Looper.preapare()
use a first inflaction to create a container of my list
use a second inflaction to create a list row
I suppose I cannot inflate two times but i don't know how i can resolve that
AsyncTask
private class DrawerView extends AsyncTask<ActivityGroup, String, View>{
Exception exc=null;
#Override protected void onPreExecute() {
super.onPreExecute();
}
#Override protected View doInBackground(ActivityGroup... params) {
try {
Looper.prepare();
return processAct();
}catch (ConnectionException e) {
exc =e;
return null;
}
catch (Exception e) {
exc = e;
return null;
}
}
#Override protected void onPostExecute(View result) {
super.onPostExecute(result);
if(exc!= null){
Utils.usrMessage(getApplicationContext(), "Oh Noo!:\n"+exc.getMessage());
Utils.logErr(getApplicationContext(), exc);
finish();
}
if(result!= null){
setContentView(result);
}
}
}
processAct() is an abstract method implemented in this way
#Override protected View processAct() throws Exception {
Bundle bundle = getIntent().getExtras();
User user = (User)bundle.getSerializable("user");
Team team = Team.getTeamInformation(this,user.getTeamId());
ArrayList<Player> players =Player.getPlayerList(this,user.getTeamId());
PlayersListAdapter view = new PlayersListAdapter(this,players,team);
return view;
}
PlayerListAdapter is the class which builds/sets first view (list container)..here the first inflation
public PlayersListAdapter(Context context, ArrayList<Player> players,Team team) throws Exception{
super(context);
View view = inflate(getContext(), R.layout.team_players, this);
TextView tv_teamName = (TextView)view.findViewById(R.id.tbplrs_tmnm);
TextView tv_playersNum = (TextView)view.findViewById(R.id.tbplrs_nplrs);
tv_teamName.setText(team.getName());
String msg = players.size()+" ";
msg += (players.size()!=1)?context.getString(R.string.playerPlural):context.getString(R.string.playerSingle);
tv_playersNum.setText(msg);
ListView lView = (ListView)view.findViewById(R.id.tbplrs_plrslst);
PlayersRowListAdapter plAdapter = new PlayersRowListAdapter(context, players);
lView.setAdapter(plAdapter);
}
at last PlayerRowListAdapter which extends BaseAdapter,...here the second inflation
#Override public View getView(int position, View view, ViewGroup parent) {
if (view == null){
LayoutInflater lInflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = lInflator.inflate(R.layout.team_player_singlplayer,null);
}
....
....
}
N.B. if i drop the second adapter PlayerRowListAdapter...all works fine...(obviously without list)
Regards
p.s. sorry for my english

Instead of just calling Looper.prepare();, first check if Looper does not already exist for your Thread, if not, call that function. Like this:
if (Looper.myLooper()==null)
Looper.prepare();

The only reason you need to call Looper.prepare() and Looper.loop() is when you want to have a message Handler in a thread that is not the UI thread. Basically, it keeps the thread alive forever so that the Handler that was created inside the thread can still send and receive messages. The same goes for callback methods like LocationListener or something similar. You are responsible for killing the thread when it is done by calling Looper.getMyLooper().quit() inside the thread that it is in.
If you are inflating views in the UI thread, then you do not need to call Looper.prepare() or Looper.loop() as this is already done in the background. You should never inflate Views outside the UI thread.

AsyncTask already has its own Looper. If you want to update your UI from your doInBackground() method use publishProgress(..) which then invokes onProgressUpdate(..) in the main thread. In your onProgressUpdate you can inflate your Views and add them to your UI.
Edit: example code: http://developer.android.com/reference/android/os/AsyncTask.html

Here's how I worked around this.
As the Developer Reference for AsyncTask says,
doInBackground creates a new thread to manage it (this has forced me to call Looper.prepare()), while onPostExecute() uses the main thread.
So I sliced processAct() in two methods: prepareData() which retrieves data and createView() which calls adapter.
I have put the first method into doInBackground(), and the second one (createView()) I have put into onPostExecute().

Related

Notify when all onCreateViewHolder inside a RecylerView have finished

RecyclerView calls onCreateViewHolder a bunch of times and then just keeps binding the data to these views. My view creation is slightly expensive and hence I need to defer rest of the UI tasks until my RecyclerView is done creating all the views.
I tried adding a ViewTreeObserver.OnGlobalLayoutListener but this callback gets called before even the first onCreateViewHolder() call.
Any idea how do I go about it?
After some research I've found out a solution with Handler. As you I'm looking for a beautiful code and this is a bit messy for me. But works perfectly anyway.
Handler is a class that you can use in a way to post message and/or Runnable, which will be added in a queue, then executed when that queue is finished.
My plan is, given that the adapter works on the UI, (inflate ect...) the creation and initialization (all onCreateViewHolder and onBindViewHolder) are added at a moment in the handler of the main thread.
That means that if you post a message in the main thread queue (the same obligatory used by your adapter), then the message will be executed after any previous request (after your adapted has finished to initialize everything).
Exemple :
Main activity
Initialization of the handler :
private Handler mHandler;
#Override
protected void onCreate(Bundle iSavedInstanceState) {
...
mHandler = new Handler(Looper.getMainLooper());
}
Initialization of your CustomAdapter :
private void initializeAdapter(...) {
MyCustomAdapter lMyNewAdapter = new MyCustomAdapter(...)
...
lNewAdapter.SetOnFirstViewHolderCreation(new
MyCustomAdapter.OnFirstViewHolderCreation {
#Override
public void onCreation() {
mHandler.post(new Runnable() {
#Override
public void run() {
// Finally here, the code you want to execute
// At the end of any Create and Bind VH of your
// Adapter
}
});
}
});
}
MyCustomAdapter
private boolean mIsViewHolderCreationStarted;
private OnFirstViewHolderCreation mOnFirstViewHolderCreation;
public CustomItemViewAdapter onCreateViewHolder(
#NonNull ViewGroup iViewGroup, int iI) {
...
if (!mIsViewHolderCreationStarted) {
mIsViewHolderCreationStarted = true;
if (mOnFirstViewHolderCreation != null) {
// It's at this point that we want to add a new request
// in the handler. When we're sure the request of the
// adapter has begun.
mOnFirstViewHolderCreation.onCreation();
}
}
}
public void setOnFirstViewHolderCreation(OnFirstViewHolderCreation iAction) {
mOnFirstViewHolderCreation = iAction;
}
public interface OnFirstViewHolderCreation {
void onCreation();
}
Note
Be aware that this solution will execute a code at the end of the first initialization of the enteer page that it is possible to show in a case of a RecyclerView.
A onCreateViewHolder might be called in case the screen is scrolled.
Which means that this solution does not guarantee you this handler message is executed after all possible onCreateViewHolder.
It only helps you to avoid an overload on the MainThread, during the greedy work of the adapter init.
Something else, in case you're using animations with your adapter to make it appears smoothly or something else (one of the good reasons to use this way to do), don't forget to put your RecyclerView in VISIBLE and not GONE, otherwise, the initialization of the adapter never happens.

Activity doesnt show until Asynctask is done

I am starting an activity and calling an asynctask in onCreateView() to fetch data from a webservice and populate the data in onPostExecute(). Currently the activity doesnt load until the asynctask in the fragment finishes.
How can i display the empty form immediately and update it as the task finishes? The activity just hosts fragments and the asyntasks are in the fragments, if that makes a difference.
Fragment onCreateView():
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_friends,container, false);
return v;
}
Fragment onActivityCreated():
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
//getFriends() calls a method that is an asyntask
peopleList = getFriends();
if (peopleList!=null){
list = (ListView)getActivity().findViewById( R.id.friendsList);
list.setAdapter(new FriendsAdapter(getActivity(), peopleList,"friends") );
}
}
This is because you are populating the data onPostExecute() which gets hooked on to the UI thread only after doInBackground() is complete. If you want to show the progress as and when things happen you need to call the publishProgress() method.
Now the publishProgress() method hooks onto the onProgressUpdate() method and this hooks onto the UI thread which updates the UI thread while doInBackground() is running. I've given a very simple example of something I did for practice sometime back - take a look at how it works. It basically keeps updating a ListActivity, one element at a time and shows this progress on screen - it does not wait until all the strings are added to display the final page:
class AddStringTask extends AsyncTask<Void, String, Void> {
#Override
protected Void doInBackground(Void... params) {
for(String item : items) {
publishProgress(item);
SystemClock.sleep(400);
}
return null;
}
#Override
protected void onProgressUpdate(String... item) {
adapter.add(items[0]);
}
#Override
protected void onPostExecute(Void unused) {
Toast.makeText(getActivity(), "Done adding string item", Toast.LENGTH_SHORT).show();
}
}
You should do as little time consuming tasks as possible in your Activity's onCreate() method. Note that your form will only been drawn after the onCreate() was 'entirely' executed.
A good idea is to start your time consuming task from a timer with a little delay (50 - 100 ms) to be sure that your onCreate() method finished executing.
Here is an example (put this instead of getFriends()):
Handler getFriendsDelayed = new Handler();
getFriendsDelayed.postDelayed(new Runnable()
{
public void run()
{
getFriends();
}
}, DELAY_IN_MILLISEC);
Hope it helps.

Waiting until user actually sees something on the screen

I've got a main Activity, an extra class for my fragment, and inside this fragment is an AsyncTask, which gathers data from various android library (Wifi SSID, BSSID, etc). When I start my app the app shows a blank screen, without any UI. Then after about 2 seconds, the whole data is being shown. I actually want to display my TextViews as "Not connected to a wifi network" in the background, while showing a ProgressDialog until the data is being displayed. I've got the ProgressDialog in my MainActivity, and calling it in my AsyncTask onProgressUpdate
MainActivity.progressDialog = ProgressDialog.show(MainActivity.c,
"ProgressDialog Title",
"ProgressDialog Body");
I'm updating my TextViews in the doInBackground methode (via another methode outside the Fragment)
((Activity) getActivity()).runOnUiThread(new Runnable() {
Would be too big a comment so i'll just put it here.
Sounds like you are using both fragment and AsyncTask in an incorrect way. You should never do anything UI relevant in doInBackground.
Here is an example of what you could do.
I assume the following scenario:
You have a main activity
You have a fragment containing TextViews
You wish to populate the TextViews after loading some data using AsyncTask with a progressDialog
The approach would be to:
Add the fragment in onCreate of your activity (if the fragment is not defined in the layout, then it will automatically be added).
Create the AsyncTask in your fragment like this:
private class LoadData extends AsyncTask<Void, Void, List<String>> {
ProgressDialog progressDialog;
//declare other objects as per your need
#Override
protected void onPreExecute()
{
// getActivity() is available in fragments and returns the activity to which it is attached
progressDialog= new ProgressDialog(getActivity());
progressDialog.setTitle("ProgressDialog Title");
progressDialog.setMessage("ProgressDialog Body");
progressDialog.setIndeterminate(true)
progressDialog.setCancelable(false)
progressDialog.show();
//do initialization of required objects objects here
};
#Override
protected Void doInBackground(Void... params)
{
List<String> results = new ArrayList<String>();
//do loading operation here
//add each of the texts you want to show in results
return results;
}
// onPostExecute runs on UI thread
#Override
protected void onPostExecute(List<String> results )
{
progressDialog.dismiss();
// iterate results and add the text to your TextViews
super.onPostExecute(result);
};
}
Start the AsyncTask in onCreate of your fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
new LoadData().execute();
super.onCreate(savedInstanceState);
}
This way you avoid calling directly back to your activity, which really should not be necessary in your scenario (unless I have misunderstood).
Otherwise please post all the relevant code and layouts.
This line:
I'm updating my TextViews in the doInBackground methode
points to your problem. You need to use the AsyncTask method onProgressUpdate() to publish to the UI thread. You do not call onProgressUpdate() directly, instead you call publishProgress().
Interestingly, I answered a similar question yesterday here: android AsyncTask in foreach
and it includes an example.
Here's what you need to do.
(1) From the place you run the code that gathers data, you should first display the progress dialog. Something like this:
busy = new ProgressDialog (this);
busy.setMessage (getString (R.string.busy));
busy.setIndeterminate (true);
busy.setCancelable (false);
busy.show();
(2) Then you start your data gathering. This must be done in a separate thread (or Runnable). Do something like this:
Thread thread = new Thread ()
{
#Override
public void run()
{
try
{
... gather data ...
Message msg = handler.obtainMessage();
msg.what = LOADING_COMPLETE;
msg.obj = null;
handler.sendMessage (msg);
}
catch (Exception e)
{
Message msg = handler.obtainMessage();
msg.what = LOADING_FAILED;
msg.obj = e.getMessage(); // maybe pass this along to show to the user
handler.sendMessage (msg);
}
// get rid of the progress dialog
busy.dismiss();
busy = null;
}
}
(3) Add a handler to the activity to receive notification when data gathering is complete:
Handler handler = new Handler()
{
#Override
public void handleMessage (Message msg)
{
if (msg.what == LOADING_COMPLETE)
loadingComplete ();
else if (msg.what == LOADING_FAILED)
loadingFailed ((String)msg.obj);
}
};
(4) Implement the handlers:
private void loadingComplete ()
{
...
}
private void loadingFailed (String errorMessage)
{
...
}
That's the essentials.

AsyncTask result differs from regular function

I have a View that recieves swipe gestures.
this should trigger the creation and setContentView(layout) for the main activity.
(the View is within a Layout on the Main activity)
When i try to do this within an asynctask it leaves out all the images for the layout?
as if it hasn't finished yet .
There is prolly something i'm not fully understanding .
this the partial code from the view.java
Main.mHandler.post(new Runnable()
{
public void run()
{
new Main.GetNavigationLayout().execute(url);
}
});
the url is a location of an .xml file I use to create a new layout.
GetNavigationLayout is the AsyncTask
code that is the AsyncTask in Main.java :
public static class GetNavigationLayout extends AsyncTask<String, Void, CustomLayout> {
boolean isDone = false;
#Override
protected EvadoLayout doInBackground(String... url)
{
CustomLayout layout = new CustomLayout(Main);
Looper.prepare();
try
{
String newpage = url[0];
Document doc = XmlImporter.GetNewPlayer(newpage);
if (doc == null)
{
layout = null;
}
layout.LoadXml(doc); // cause drawing of objects etc.
}
catch (Exception e)
{
e.printStackTrace();
}
isDone = true;
//Looper.loop(); // causes it to never return...
return layout;
}
#Override
protected void onPostExecute(CustomLayout layout)
{
if (isDone)
Main.setContentView(layout);
}
}
now this shows everythig besides images , whereas if i run this without AsyncTask it displays everything in the layout.
what am i missing?
layout.LoadXml(doc); // cause drawing of objects etc.
I reckon you are drawing the images here? I think this is the problem. Trying to draw from the AsyncTask's doInBackground() is wrong, since it is not the UI thread. You should do the drawing in onPostExecute() which runs in the UI thread
You can post to the UI in the onProgressUpdate(). Using this method will allow you to stay inside the doInBackground and post an update to the UI when you get a layout and continue inside the doInBackground. Use publishProgress() to send information from the doInBackground to the UI thread.

Understanding the Handler in ListActivity

I'm looking at the ListActivity source code, and I'm seeing that a private Handler is being defined, and that a Runnable is posted to this handler in the onContentChanged() method.
I don't quite get the point of this, as the handlers, as I understand it, are there for inter-thread communication. Here, the definition of the handler and the posting is happening on the same thread, and no delay is specified in the post() call. I can't see the handler being used for anything else, either.
I've probably misunderstood something about the use of handlers here. Why is it done the way it is here, and not by just running mList.focusableViewAvailable() (the call inside the runnable) directly? Wouldn't the result be the same?
Beneath is what I believe are the relevant portions of the ListActivity source code:
public class ListActivity extends Activity {
protected ListView mList;
private Handler mHandler = new Handler();
private Runnable mRequestFocus = new Runnable() {
public void run() {
mList.focusableViewAvailable(mList);
}
};
/**
* Updates the screen state (current list and other views) when the
* content changes.
*
* #see Activity#onContentChanged()
*/
#Override
public void onContentChanged() {
super.onContentChanged();
View emptyView = findViewById(com.android.internal.R.id.empty);
mList = (ListView)findViewById(com.android.internal.R.id.list);
if (mList == null) {
throw new RuntimeException(
"Your content must have a ListView whose id attribute is " +
"'android.R.id.list'");
}
if (emptyView != null) {
mList.setEmptyView(emptyView);
}
mList.setOnItemClickListener(mOnClickListener);
if (mFinishedStart) {
setListAdapter(mAdapter);
}
mHandler.post(mRequestFocus);
mFinishedStart = true;
}
}
Why is it done the way it is here, and not by just running mList.focusableViewAvailable() (the call inside the runnable) directly? Wouldn't the result be the same?
Your concern should not be the Handler. Your concern should be the call to post(). A Handler is not even really needed, as post() is available on View -- this code may pre-date that, though.
post() takes a Runnable and puts it on the message queue for the main application thread. As such, it will not get processed until all other messages that are presently on that queue get processed (FIFO). Presumably, ListActivity needs some other message on the queue to be processed first before focusableViewAvailable() will work successfully.

Categories

Resources