0

I've an app that opens the facebook app when you click on a button, it works fine but on slow devices (like mine) facebook takes some seconds to show up, so i want to add a simple progressdialog that says "please wait"

i can show the progress dialog and open facebook with this code:

final ProgressDialog pd = ProgressDialog.show(contatti.this, "", "Attendere...", true);
            Intent facebookIntent = getOpenFacebookIntent(getApplicationContext());
            startActivity(facebookIntent);

            //pd.dismiss();

the first time i tried, it worked fine but when i went back from facebook to my app the dialog was still showing, and i had no way to close it.

added dismiss() to try hide it, but it was a stupid idea >.<

how can i dismiss the dialog when the app regain control?

jack_the_beast
  • 1,838
  • 4
  • 34
  • 67

3 Answers3

1

For this situation you have to check whether the application is sent background or not in on pause if it sent to background then close the dialog.

for checking the application is in bacground or not just have a look

android:how to check if application is running in background

Community
  • 1
  • 1
android_dev
  • 1,477
  • 12
  • 18
0
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import edu.gvsu.cis.toptracks.R;
import edu.gvsu.cis.toptracks.TopTrackListActivity;
import edu.gvsu.cis.toptracks.data.Customer;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class LastWebAPITask extends AsyncTask<String, Integer, String> {
    private ProgressDialog progDialog;
    private Context context;
    private TopTrackListActivity activity;
    private static final String debugTag = "LastWebAPITask";

    public LastWebAPITask(TopTrackListActivity activity) {
        super();
        this.activity = activity;
        this.context = this.activity.getApplicationContext();
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progDialog = ProgressDialog.show(this.activity, "Search", this.context
                .getResources().getString(R.string.looking_for_tracks), true,
                false);
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            Log.d(debugTag, "Background:" + Thread.currentThread().getName());
            String result = LastHelper.downloadFromServer(params);
            return result;
        } catch (Exception e) {
            return new String();
        }
    }

    @Override
    protected void onPostExecute(String result) {

        ArrayList<Customer> trackdata = new ArrayList<Customer>();

        progDialog.dismiss();
        if (result.length() == 0) {
            this.activity.alert("Unable to find track data. Try again later.");
            return;
        }

        try {
            JSONObject respObj = new JSONObject(result);
            JSONArray tracks = respObj.getJSONArray("GetStockListResult");
            for (int i = 0; i < tracks.length(); i++) {
                JSONObject track = tracks.getJSONObject(i);
                String Inventory_Status = track.getString("Inventory_Status");
                String modify_Date = track.getString("modify_Date");
                String msg = track.getString("msg");
                String serial_nbr = track.getString("serial_nbr");
                ;

                trackdata
                        .add(new Customer(Inventory_Status, modify_Date, msg, serial_nbr));
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        this.activity.setTracks(trackdata);

    }
}


    import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

public class LastHelper {

    private static final String LastUrl = "http://etosxmldev.ctdi.com/ws/wcf/UNIVERSAL-DEMO/Service.svc/GetStockList?LocationId=300779360.svc/GetStockList/1111";
    private static final int HTTP_STATUS_OK = 200;
    private static byte[] buff = new byte[1024];
    private static final String logTag = "LastHelper";

    public static class ApiException extends Exception {
        private static final long serialVersionUID = 1L;

        public ApiException(String msg) {
            super(msg);
        }

        public ApiException(String msg, Throwable thr) {
            super(msg, thr);
        }
    }

    protected static synchronized String downloadFromServer(String... params)
            throws ApiException {
        String retval = null;

        String url = LastUrl;

        Log.d(logTag, "Fetching " + url);

        // create an http client and a request object.
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);

        try {

            // execute the request
            HttpResponse response = client.execute(request);
            StatusLine status = response.getStatusLine();
            if (status.getStatusCode() != HTTP_STATUS_OK) {
                // handle error here
                throw new ApiException("Invalid response from last.fm"
                        + status.toString());
            }

            // process the content.
            HttpEntity entity = response.getEntity();
            InputStream ist = entity.getContent();
            ByteArrayOutputStream content = new ByteArrayOutputStream();

            int readCount = 0;
            while ((readCount = ist.read(buff)) != -1) {
                content.write(buff, 0, readCount);
            }
            retval = new String(content.toByteArray());

        } catch (Exception e) {
            throw new ApiException("Problem connecting to the server "
                    + e.getMessage(), e);
        }

        return retval;
    }
}

U have to use Asynctask class for doing this.Working Example for me.in your Activity class

 LastWebAPITask lfmTask = new LastWebAPITask(
                        TopTrackListActivity.this);
lfmTask.execute(metroTxt);
0

thanks to another forum. looks like the fastest and simple way to do this is using onResume():

@Override
public void onResume(){
    super.onResume();
    if(waitdialog != null){ //check if we are resuming (not coming from another activity)
        if (waitdialog.isShowing()) { //check if is showing
            waitdialog.dismiss(); //dismiss
        }
    }
}
jack_the_beast
  • 1,838
  • 4
  • 34
  • 67