playmaker420
playmaker420

Reputation: 1537

How to stay in an activity if the Internet connection is lost in between an asynchorous task

I have a button in an activity. When I press it, it will load another activity. In this activity I have a custom grid view with a description and an image in each cell. I am populating the grid view from a remote server through an asynchronous task:

button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

        if (isInternetAvailable) {
            Intent intent = new Intent(menuActivity.this,
                    gridActivity.class);
            startActivity(intent);
        }
        else {
            MyAlertDialog.ShowAlertDialog(menuActivity.this,
                                          "",
                                          "No network connection",
                                          "OK");
        }
    }
});

I am sharing the testAsyncTask() here. I am calling this function in the onCreate() of gridActivity (the second activity)

private void testAsyncTask() {
    new AsyncTask<Object, Object, Object>() {

        @Override
        protected void onPreExecute() {

            progress_Dialog = ProgressDialog.show(a, "", "Loading");
        }

        @Override
        protected Integer doInBackground(Object... params) {
            try {
                MenuService menuService = new MenuServiceImpl();
                MenuServiceResponse pMenu = menuService.getMenu();
                itemlist = pMenu.getMenu().getMenus();
                return 0;
            }
            catch (MyServiceException e) {
                MyAlertDialog.ShowAlertDialog(GridActivity.this, "", "No network connection detected.", "OK");
                e.printStackTrace();
            }
            return 0;
        }

        @Override
        protected void onPostExecute(Object result) {

            if (progress_Dialog != null) {
                progress_Dialog.dismiss();
            }

            try
            {
                adapter = new GridAdapter(
                    GridActivity.this, itemlist);
                AllitemgridView.setAdapter(adapter);

            }
            catch(Exception e)
            {
                adapter = null;
                MyAlertDialog.ShowAlertDialog(GridActivity.this, "", "Check Network Connection", "OK");
            }

        }
    }.execute();
}

I am checking a condition in the first activity where I kept the button. I will fire the start activty only if I have an Internet connection.

Now in to my problem, if I have an Internet connection and I press the button, startacvitiy will fire, and it will move to the second activity. In the onCreate() of the second activity I'm calling the testAsync(). Now it will show a loading dialog box and in the background I will fetch the data from a remote server.

Now if I switch my modem off, the application freezes for 5-6 seconds and then it will force close. So how can I handle this exception?

The coding inside the doInbackgorund() requires an Internet connection and that could be the reason for this crash when I disconnect from Wi-Fi. How can I handle this crash?

Upvotes: 0

Views: 322

Answers (1)

Jamshid
Jamshid

Reputation: 340

You can't use this in doinbackground method, because this method is doing another thread, MyAlertDialog.ShowAlertDialog(GridActivity.this, "", "No network connection detected.", "OK");

You can only use it in the onpost method.

Or you can use the dialog message:

    @Override
    protected Integer doInBackground(Object... params) {
        try {
            MenuService menuService = new MenuServiceImpl();
            MenuServiceResponse pMenu = menuService.getMenu();
            itemlist = pMenu.getMenu().getMenus();
            return 0;
        } catch (MyServiceException e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    MyAlertDialog.ShowAlertDialog(GridActivity.this, "", "No network connection detected.", "OK");
                }
            });
            e.printStackTrace();
        }
    }

Upvotes: 1

Related Questions