Android Question Persist webview through orientation change

barx

Well-Known Member
Licensed User
Longtime User
Is there a way in b4a to persist a webview when the orientation is changed?

We can obviously get the url and reload it, but what if the user has entered some data and then decides to rotate. All is lost.

Searching on net shows the use of manifest edit
B4X:
android:configChanges="orientation|screenSize"

but it requires

B4X:
onconfigurationchange()

something that doesn't seem to be inplemented in b4a

Another option documented in java is to

B4X:
  protected void onSaveInstanceState(Bundle outState) {
      webView.saveState(outState);
  }

and

B4X:
public void onCreate(final Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.blah);
  if (savedInstanceState != null)
      ((WebView)findViewById(R.id.webview)).restoreState(savedInstanceState);
}

If this method is possible with JavaObject or Reflector, I have no idea how to do it.

Ideas?
 

Baris Karadeniz

Active Member
Licensed User
I wrote the LoadLayout line to the AfterChange sub as below but everything is mixed at the screen. Should I write something more?

B4X:
Sub AfterChange
   Dim ajo As Panel = Activity
   Dim width As Int = ActivityParent.RunMethod("getMeasuredWidth", Null)
   Dim height As Int = ActivityParent.RunMethod("getMeasuredHeight", Null)
   If width = 0 Or height = 0 Then Return
   ajo.Width = width 'update the "activity" width and height
   ajo.Height = height
   WebView1.Width = width
   WebView1.Height = height
   Activity.LoadLayout("Layout1")
End Sub
 
Upvote 0

Baris Karadeniz

Active Member
Licensed User
As I previously wrote, I recommend you to avoid using the solution from this thread.
It breaks the standard program flow. It requires better knowledge of B4A and Android.

Call Activity.RemoveAllViews before you load the new layout.

Erel, my mind is confused. Because you wrote the statement below at #18;

You can use this code. However you will need to manually update the layout. Another possible solution is to lock this specific activity orientation.

And also you wrote the statement below at #20;

You need to update the layout in AfterChange sub.


But now you say again avoid using the solution from this thread.

Can you please explain Can I use the codes in this thread with updating layout manually together or not? And if it is yes, can you please give example codes?
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
You can use this code. However you will need to manually update the layout.
If you disable the ability for the Activity to be destroyed and recreated when the screen is rotated, than you will have to find a way to resize your layout at runtime after having checked that the dimensions changed (that is done with the IME).
Another possible solution is to lock this specific activity orientation.
You can lock the Activity's orientation so when the device is rotated, the screen does not change its orientation.

For example:
B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
   #SupportedOrientations: portrait
#End Region

You need to update the layout in AfterChange sub.
If you don't lock the Activity's orientation, YES, you will have to manage the dimensions of your Views by code at runtime.

But now you say again avoid using the solution from this thread.
This is because it will make managing your application more difficult. You then have to well understand the Activity's life process for many reasons : views dimensions, anchors... You won't go through Activity_Create and Activity_Resume

Can I use the codes in this thread with updating layout manually together or not?
Of course you can if you want to manage your views and your layout manually, who can ban you from doing it ?

And if it is yes, can you please give example codes?
Make the trial by yourself : add a view to your layout, block the orientation. Detect the screen's dimensions changes using the IME HeightChanged event and try to move and resize your views
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…