Varnit Khandelwal
Varnit Khandelwal

Reputation: 121

I want to refresh/recreate my activity programatically when getting response from server

I want when server sends some response in form of WebView then immediately my activity gets refreshed and so WebView in form of banner ad. I write code for display banner ad but ad is showing only when my activity recreated i.e. when I rotate my screen then banner is showing but when it is in same static mode then banner is not showing. So, please let me know what I will do so that when server gave some response immediately it will be shown on my activity.

void startDemo() {

    //Set Http Client Options
    final OptimusHTTP client = new OptimusHTTP();
    client.enableDebugging();
    client.setMethod(OptimusHTTP.METHOD_POST);
    client.setMode(OptimusHTTP.MODE_SEQ);


    FreqDetector_Goertzel.getInstance().startRecording(new FreqDetector_Goertzel.RecordTaskListener() {
        private String urlRedirect = "";
        private String imgSmallBanner = "";

        @Override
        public void onSuccess(int val)
        {
                String pSet = pVal.getPatternSet(val, 5);
                if (pSet != null) {
                    FreqDetector_Goertzel.getInstance().stopRecording();
                    EasyDeviceInfo deviceInfo = new EasyDeviceInfo(MainActivity.this);

                    final HashMap<String, String> device_params = new HashMap<>();
                    device_params.put("aid", deviceInfo.getAndroidID());
                    device_params.put("pattern", pSet);

                    if (isNetworkAvailable(MainActivity.this)) {
                        try {
                            client.makeRequest(MainActivity.this, new HttpReq(), Defaults.MATCHINGSERVER, device_params, new OptimusHTTP.ResponseListener() {
                                @Override
                                public void onSuccess(String s) {
                                    try {
                                        if (s != null && !s.contains("No Match Found"))

                                        {
                                            JSONObject jsonObject = null;
                                            jsonObject = new JSONObject(s);
                                            imgSmallBanner = Uri.decode(jsonObject.optString("smallImgUrl", "NA"));
                                            urlRedirect = Uri.decode(jsonObject.optString("redirectUrl", "NA"));
                                            loadAdvertisement(urlRedirect, imgSmallBanner);
                                        } else {
                                            //Did not match
                                            startDemo();
                                        }

                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }

                                }

                                @Override
                                public void onFailure(String s) {

                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        //Internet not available. Do not do anything.
                    }
                }

        }

        @Override
        public void onFailure(String s) {
        }
    });
}

void loadAdvertisement(String clickUrl, String imgSmallName) {
    String click_url;
    String img_small_url;

    stopDemo();

    click_url = Uri.decode(Uri.encode(clickUrl));
    img_small_url = imgSmallName;

    StringBuilder htmlData2 = new StringBuilder();
    htmlData2.append("<html><body style='margin:0;padding:0;background-color:black;'><a href='").append(click_url).append("' ><img src='").append(img_small_url).append("' height=50 style='margin:0 auto;display:block;' /></a></body></html>");
    webView_img_small.loadDataWithBaseURL("file:///android_asset/", htmlData2.toString(), "text/html", "utf-8", null);
    webView_img_small.setVisibility(View.VISIBLE);
 /* What I will do here so when server sends response it will immediately being refreshed and shown on activity without recreating it.*/ }

Upvotes: 0

Views: 87

Answers (2)

justHooman
justHooman

Reputation: 3054

You can try to use Activity.recreate(). This method will destroy your current Activity and create a new Activity same way when you rotate device.
Hope this helps.

Upvotes: 0

Mayur R. Amipara
Mayur R. Amipara

Reputation: 1223

here you can find some response: http://developer.android.com/guide/topics/ui/how-android-draws.html

for me a call to invalidate() only refresh the view and a call to requestLayout() refresh the view and compute the size of the view in the screen.

Upvotes: 1

Related Questions