Preventing Internet-accessing-method from delaying a toast popup


Quite new to Android development and Java in general, so please excuse any amateur ignorance and lack of terminology.

I'm working on an Android app that involves fetching Web pages as strings, using a method based on the code available at <a href="http://www.spartanjava.com/2009/get-a-web-page-programatically-from-android/" rel="nofollow">http://www.spartanjava.com/2009/get-a-web-page-programatically-from-android/</a>.

This takes a small but noticeable amount of time, but works fine. It is triggered by pressing a button in the UI. Since the application is unresponsive while data is being fetched, I've got a toast that is meant to warn users before it happens.

Here is essentially what is being done (not the actual code, just illustrative):

public void buttonPressed(View view) { Toast.makeText(this, "Getting Data!", Toast.LENGTH_LONG).show(); //See the page linked above for the code in this function! String page = getPage("http://www.google.com/"); Toast.makeText(this, "Data Retrieved!", Toast.LENGTH_LONG).show(); }

Unfortunately, The "Getting Data" toast only seems to appear after the getPage method has completed, appearing very briefly before being covered up by the "Data Retrieved" toast.

How do I avoid this, making the "Getting Data" toast appear, then the getPage method run, then the "Data Retrieved" toast appear when the method terminates?

Any suggestions would be much appreciated. I expect the solution involves some kind of threads or synchronisation, but don't even know where to start looking for an appropriate tutorial...



correct use of an AsyncTask class that solves your problem:

notice the onPreExecute and onPostExecute methods which are called before/after your get the page.

public class HomeActivity extends Activity { public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.home); } public void buttonPressed(View view) { new MyAsyncTask(this).execute(new String[] {"http://google.com/"}); } private class MyAsyncTask extends AsyncTask<String, Void, String> { private Context context; public MyAsyncTask(Context context) { this.context = context; } @Override protected String doInBackground(String... params) { String page = getPage(params[0]); //do any more work here that may take some time- like loading remote data from a web server, etc return page; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); Toast.makeText(context, "Data Retrieved: " + result, Toast.LENGTH_LONG).show(); } @Override protected void onPreExecute() { super.onPreExecute(); Toast.makeText(context, "Getting Data!", Toast.LENGTH_LONG).show(); } } }


You shouldn't be performing long running (ie. network or disk I/O) operations in the UI thread. You should use an AsyncTask or a Thread/Handler combination.

Here are some links:

<ul><li><a href="http://developer.android.com/resources/articles/painless-threading.html" rel="nofollow">Painless Threading</a></li> <li><a href="http://developer.android.com/reference/android/os/AsyncTask.html" rel="nofollow">AsyncTask</a></li> <li><a href="http://developer.android.com/reference/java/lang/Thread.html" rel="nofollow">Thread</a></li> </ul>


  • export data in datagridview to excel sheet windows form
  • iPhone/iPad: animated splash screen?
  • Task queue for wp8?
  • google map and api shows different results
  • Filter a directory for .csv files using c# lambda
  • Add a new custom default ordering catalog option in Woocommerce
  • ProgressDialog.show inside AsyncTask stop my Program from execution
  • Mailkit Authenticate to Imap fails
  • ASIO UDP: class std::allocator has no member named ‘construct’
  • CodeIgniter shared data between calls to load->view
  • Starting Second tab fragment of first activity from second activity
  • NOTE or WARNING from package check when README.md includes images
  • Multiple JSliders reacting to each other to always equal 100 percent
  • Train two models concurrently
  • Draw ring with given thickness, position, and radius. (Java2D)
  • Multiple git user in single device
  • SqlDatasource select parameters
  • Spring annotation @Order
  • can you use embedded ruby in custom javascript files in rails?
  • PowerShell script to pass SecureString to Plink as account and sudo passwords
  • Unity Resources.load() won't work with external dll
  • Get name of days between two date in ios?
  • How to display content depending on dropdown menue user selection
  • How to find angle formed by the blades of a wind turbine with respect to a horizontal imaginary axis
  • Does hibernate load two seprate copies of same instance if they are loaded twice from database?
  • how to specify different css for ie
  • Silverlight Event Log in Isolated Storage
  • Find angle of point on circle
  • How can i move Clearcase dyamic/snapshot views to another host (Linux)
  • DataTables move rows between tables
  • How to use Streams api peek() function and make it work?
  • Will this work on all screen sizes?
  • Codeigniniter insert data through models and controller
  • Jersey serializes character value to ASCII equivalent numeric string
  • Spring Boot not autowiring @Repository
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}