2

I want to create a WebView in onCreate() method of a derivative of Application class, then attach it to the main layout when an activity onCreate() is called and detach it when onDestroyed() is called. So, every time when an activity is being created/destroyed, the WebView component will be the same (kinda singleton). The problem is I (with my Windows API background) have no ideas how to do this. Just new WebView()/addiew()/removeView()?

Why do I want to do this, you asked? Prevent Android activity from being recreated on turning screen off In several words, the WebView should never be destroyed.

Community
  • 1
  • 1
noober
  • 4,819
  • 12
  • 49
  • 85

1 Answers1

4

Nothing special. Register MyApp as application class name in the manifest.

public class MyApp extends Application
{
    public WebView _WebView = null;

    @Override
    public void onCreate()
    {
        _WebView = new WebView(getApplicationContext());
        // Settings etc.
        _WebView.loadUrl("url");

        super.onCreate();
    }
}

Remove the view from main.xml.

public class MyActivity extends Activity
{
    WebView _WebView;
    RelativeLayout _Layout; // Should be declared in main.xml.

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        _Layout = (RelativeLayout) findViewById(R.id.rl);
        ViewTreeObserver vto = _Layout.getViewTreeObserver(); 
        vto.addOnGlobalLayoutListener(new MyLayoutListener()); // .layout(0,0,width,height);

        Display display = getWindowManager().getDefaultDisplay();
        MyApp app = (MyApp) this.getApplication();
        _WebView = app._WebView;
        _Layout.addView(_WebView, display.getWidth(), display.getHeight());
    }

    @Override
    protected void onDestroy()
    {
        _Layout.removeView(_WebView);
        super.onDestroy();
    }
}

private class MyLayoutListener implements OnGlobalLayoutListener
{
    public void onGlobalLayout()
    {
        Display display = getWindowManager().getDefaultDisplay();
        _WebView.layout(0, 0, display.getWidth(), display.getHeight());
        //_Layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    }
}
noober
  • 4,819
  • 12
  • 49
  • 85
  • What about this line? "vto.addOnGlobalLayoutListener(new MyLayoutListener());" How to code this? MyLayoutListener doesn't exist. – ElSajko May 05 '15 at 12:46
  • @ElSajko I'm out of context now as 3 years have passed. The MyLayoutListener code is added to the auto-answer, but I will not be able to explain, why and how did I do that. Anyway, the code is for 2.3, maybe something should be done another way these days. – noober May 05 '15 at 13:16
  • I've done it, working without ViewTreeObserver. But still helpfull post. EDIT: Mea Culpa. Didn't noticed that code has scroll and there was MyLayoutListener. Nvm – ElSajko May 05 '15 at 14:05