Android. Fragment getActivity() sometimes returns null

It seems that I found a solution to my problem. Very good explanations are given here and here. Here is my example: pulic class MyActivity extends FragmentActivity{ private ViewPager pager; private TitlePageIndicator indicator; private TabsAdapter adapter; private Bundle savedInstanceState; @Override public void onCreate(Bundle savedInstanceState) { …. this.savedInstanceState = savedInstanceState; pager = (ViewPager) findViewById(R.id.pager);; indicator = … Read more

Using the “animated circle” in an ImageView while loading stuff

Simply put this block of xml in your activity layout file: <RelativeLayout android:id=”@+id/loadingPanel” android:layout_width=”match_parent” android:layout_height=”match_parent” android:gravity=”center” > <ProgressBar android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:indeterminate=”true” /> </RelativeLayout> And when you finish loading, call this one line: findViewById(R.id.loadingPanel).setVisibility(View.GONE); The result (and it spins too):

Is AsyncTask really conceptually flawed or am I just missing something?

How about something like this: class MyActivity extends Activity { Worker mWorker; static class Worker extends AsyncTask<URL, Integer, Long> { MyActivity mActivity; Worker(MyActivity activity) { mActivity = activity; } @Override protected Long doInBackground(URL… urls) { int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += … Read more

Running multiple AsyncTasks at the same time — not possible?

AsyncTask uses a thread pool pattern for running the stuff from doInBackground(). The issue is initially (in early Android OS versions) the pool size was just 1, meaning no parallel computations for a bunch of AsyncTasks. But later they fixed that and now the size is 5, so at most 5 AsyncTasks can run simultaneously. … Read more

How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

Easy: Create interface class, where String output is optional, or can be whatever variables you want to return. public interface AsyncResponse { void processFinish(String output); } Go to your AsyncTask class, and declare interface AsyncResponse as a field : public class MyAsyncTask extends AsyncTask<Void, Void, String> { public AsyncResponse delegate = null; @Override protected void … Read more

How to display Toast in Android?

In order to display Toast in your application, try this: Toast.makeText(getActivity(), (String)data.result, Toast.LENGTH_LONG).show(); Another example: Toast.makeText(getActivity(), “This is my Toast message!”, Toast.LENGTH_LONG).show(); We can define two constants for duration: int LENGTH_LONG Show the view or text notification for a long period of time. int LENGTH_SHORT Show the view or text notification for a short period … Read more

Android basics: running code in the UI thread

None of those are precisely the same, though they will all have the same net effect. The difference between the first and the second is that if you happen to be on the main application thread when executing the code, the first one (runOnUiThread()) will execute the Runnable immediately. The second one (post()) always puts … Read more