0

When I rotate my devices, this charge again his class. For example, if I have a google search when I change the orientation charges again to google.

I've try it with:

@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.principal);
}

and onRestoreInstanceState and onSaveInstanceState. But my problem persist. I would appreciate if somebody can help me with a example or explication of how can to do it.

Thank's!


I've solved the problem, I needed to do the if (savedInstanceState == null) :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.principal);

    webview.setWebViewClient(new WebViewClient());

    if (savedInstanceState == null)
    {
        webview.loadUrl("http://www.apple.es");
    }
}

  @Override   
    protected void onSaveInstanceState(Bundle outState)
    {  
      super.onSaveInstanceState(outState);  
      // Save the state of the WebView    
      webview.saveState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)  
    {  
      super.onRestoreInstanceState(savedInstanceState);
      // Restore the state of the WebView
      webview.restoreState(savedInstanceState);
    }

I hope than my problem and my solution can help to somebody!

Christian
  • 53
  • 1
  • 8
  • It is the same question like this post http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android – funcoder May 17 '12 at 08:08
  • In these cases I just set the orientation and disable the auto rotation ...Do you really need auto rotation ? – moujib May 17 '12 at 08:34

3 Answers3

0

Add configChanges in your AndroidManifest file. For example:

<activity android:name="YourActivityName"
          android:configChanges="orientation">
</activity>

and save your current url in local variable and load it in onConfigChanges

@Override
public void onConfigurationChanged(Configuration newConfig) {
    yourWebView.loadUrl(yourUrl);
}
Roman Black
  • 3,501
  • 1
  • 22
  • 31
0

use following piece of code

public void onConfigurationChanged(Configuration newConfig) {
    yourWebView.loadUrl(yourUrl);
}

Refer this link Activity restart on rotation Android

Community
  • 1
  • 1
  • Hello niks, I don't want open again the url of the page, I need save the configuration actual page that there are before the orientation change for after put it again. – Christian May 17 '12 at 09:01
  • ok then remove yourWebView.loadUrl(yourUrl); line from ur code –  May 17 '12 at 09:07
0

Christian you have to see this doc on android developer web site and learn how romain guy stores his last loaded image and call them when the configuration screen changes!! the solution is to use

  1. onRetainNonConfigurationInstance()
  2. getLastNonConfigurationInstance()

I also refer to this tutorial always to get rid of the screen change behavour of android

hope this will help you

K_Anas
  • 31,226
  • 9
  • 68
  • 81