0

In the following code, main xml file activity_main.xml is not used. What should be modified if I want to use it. The main intention is to load the webview in background while showing splashscreen on the front. Is my approach correct? If I want to use AsyncTask to load webview while showing splashscreen, what should I do? My app is loading everytime while changing the orientation. what should I do in order to fix it?

public class MainActivity extends Activity {
WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_splash);

    webview = new WebView(MainActivity.this);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setLoadsImagesAutomatically(true);
    webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    webview.getSettings().setAppCacheEnabled(false);
    webview.loadUrl("http://www.nricabs.com");
    webview.setWebViewClient(new WebViewClient(){
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if(url.startsWith("tel:")) { 
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                startActivity(intent); 
                return true;
            }
            return false;

        }
        public void onPageFinished(WebView view,String url){
            super.onPageFinished(view, url);
            setContentView(webview);
        }
    }
            );

}
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
            if(webview.canGoBack()){
                webview.goBack();
            }else{
                finish();
            }
            return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}


}
Krishna
  • 601
  • 2
  • 9
  • 32
  • "My app is loading everytime while changing the orientation. what should I do in order to fix it?" Have a look at SavedInstanceState: http://stackoverflow.com/questions/151777/saving-activity-state-in-android – Prexx Sep 29 '15 at 11:13

2 Answers2

1

About your webview question, im pretty sure that it already loads on a separate thread so you dont need to use AsyncTask.

About the reload on orientation change, you can restore the state via SavedInstanceState as shown here.

Or, you can disable it as shown here

About using a xml layout just call

 setContentView(R.layout.activity_main);

Hope this helps.

Community
  • 1
  • 1
Nanoc
  • 2,381
  • 1
  • 20
  • 35
  • setContentView(R.layout.activity_main) is not working here. I used'`WebView webview=new WebView(MainActivity.this)` instead of relating the webview ID like `webview=(WebView)findViewById(R.id.webview)`. I used this code initially. But the app crashed. – Krishna Sep 29 '15 at 11:29
  • calling setContentView with your layout should remove the webview and set the layout of the xml, isnt that correct? just like you are doing replacing the R.layout.activity_splash with the webview – Nanoc Sep 29 '15 at 11:30
  • Yes. It should present the layout with the result of loaded webview. – Krishna Sep 29 '15 at 11:32
  • sorry if I'm ambiguous. in my code I'm using setContentView with webviewe instance. If I have to use my layout file instead, what are the changes that I have to make? – Krishna Sep 29 '15 at 11:38
  • Just include a webview in your layout, then, AFTER you have called setContentView with that layout you can get the reference to it with findViewById, if you have any problem with that let me know and i will update the answer – Nanoc Sep 29 '15 at 11:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90883/discussion-between-krishna-and-nanoc). – Krishna Sep 29 '15 at 11:50
  • I think there is something waiting for your help. [please come here](http://stackoverflow.com/questions/32881992/google-maps-v2-not-loading-error-android-view-inflateexception-binary-xml-file/32882156#32882156) – Krishna Oct 01 '15 at 07:50
0

After the page has finished loading you can open a new intent to a new activity and there you can use a different layout.

Another approach could be to have one layout for both (main and progress) and then hide/show the layouts when the page has finished loading.

Thomas R.
  • 7,988
  • 3
  • 30
  • 39
  • Im sure you dont need to use a Intent to change the activity layout. – Nanoc Sep 29 '15 at 11:23
  • This is an option. Read carefully. – Thomas R. Sep 29 '15 at 11:32
  • @ThomasR. can you explain clearly the second approach with an example. Sorry for that. Understand beginners problems. – Krishna Sep 29 '15 at 11:35
  • You can create one layout. Having a frame layout as root. As child you have a splash layout, which is visible by default, and a content layout, which is gone by default. Give them Ids and after the web page finished loading you set splash.setVisibility(View.GONE) and content.setVisibility(View.VISIBLE). – Thomas R. Sep 29 '15 at 11:41