0

My android app is set to fetch data from a sever when it receives a notification from the server. This notification contains an ID which it uses to query the server and download data. The problem is however, if a user clicks on the notification before the data has been downloaded, there is a null pointer exception.

I'm looking for user friendly way to manage this, ideally if the user clicks on this before the data is downloaded then a loading symbol appears until it has been downloaded.

I don't have a concrete idea of doing this, are there any tutorials/example code regarding this problem? I guess I'll need to be using a listener?

Thanks!

supersam654
  • 3,126
  • 33
  • 34
User24231
  • 506
  • 1
  • 7
  • 20

1 Answers1

1

this is a way:

 public void showProgressDialog() {
            runOnUiThread(new Runnable() {
                public void run() {
                    mProgressDialog = onCreateProgressDialog();
                    mProgressDialog.show();
                }
            });

}

private ProgressDialog onCreateProgressDialog() {
    ProgressDialog dialog = new ProgressDialog(RequesterActivity.this);
    dialog.setTitle(getString(R.string.label_dialog_item_to_find) +" "+
            ((EditText) findViewById(R.id.edit_text_item_id)).getText().toString());
    dialog.setMessage(getString(R.string.label_dialog_finding_item));
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    return dialog;
}

Note that you could give a "no Style" to the dialog like in here Dialog with transparent background in Android or here android dialog transparent depending on what you need.

I on the other hand use the ProgressDialog with some info in it like "Please wait while loading item...". After you get the Data from the server just call the cancel() method for the ProgressDialog being showed (this ProgressDialog should be created in an instance variable, so you keep a track of it and could call cancel()from everywhere) then show whatever you will show with the Data you now have.

As a plus, since your download is a background process, you could use the Observer Pattern (Listener Pattern) where your Activity will implement a let's say AsyncTaskListener interface where AsyncTask is the name of the class of your download background process, this interface should have the methods that the Activity will call on the background process, let's say a method like cancelProgressDialog() that will be called in the onPostExecute() of your AsyncTask class, the last thing you need to do is create an instance variable (member) of the interface in your AsyncTask class and maybe by the Constructor or a Setter method set the interface to it. Here is an Example:

Interface:

public interface MessageHandlerListener {
public void toastMessage(int messageId);
}

Activity:

public class AnyActivityThatNeedsToShowAMessageInABackgroundProcessOrInUIThread extends Activity implements MessageHandlerListener {
    public void toastMessage(int messageId){
        //show a toast implementation
}
}

AsyncTask:

public class BackgroundProcess extends AsyncTask {
    MessageHandlerListener mMessageHandlerListener;

public BackgroundProcess(MessageHandlerListener m){
    mMessageHandlerListener = m;
}

 protected Object doInBackground(Object[] objects){
    return null
}

 protected void onPostExecute(Object o) {
   mMessageHandlerListener.toastMessage(Message.CONSTANT_MESSAGE);
}
}

This is a way better aproach and let you apply the MVC pattern in android where your background process could be the Controller, your Activity(s) the View and have a good Model depending on the needs of the project. Regards.

Community
  • 1
  • 1
jac1013
  • 408
  • 3
  • 11