0

I am trying to complete my org's first app (we lost our dev due to health issues) and it seems to be working except that when the phone is rotated it refreshes back to the login screen. I realize that this question was answered previously here but this information is 4 years old and I get some compiling errors.

Here is our code - I really hope that someone can help as I am completely new to this and not really a dev myself, but I feel like what we have is close to a solution.

Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ca.ruffarc.flytest">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Main Activity:

package ca.ruffarc.flytest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    private WebView webView;

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

        webView = (WebView) findViewById(R.id.webview);
        webView.setWebViewClient(new MyBrowser());

        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.loadUrl("https://flyapp.ca/en/authentication");
    }

    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        else
        {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }


}
Community
  • 1
  • 1
Rodolfo
  • 33
  • 1
  • 6
  • study `onconfigurationchange` – Pavneet_Singh Nov 22 '16 at 15:25
  • ad to the activity containing the webview this line in the manifest: android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize That way the wabview wont re create – Michael A Nov 22 '16 at 15:33
  • @MichaelA So I would simply add this line? android:configChanges="keyboard|keyboardHidden|orientation|s‌​creenLayout|uiMode|s‌​creenSize|smallestSc‌​reenSize" to the manifest? That's it? – Rodolfo Nov 22 '16 at 15:41
  • If you don't need to rotate means, You can set in activity **android:screenOrientation="portrait"** – Srihari Nov 22 '16 at 16:39
  • @SriHari I do need to support rotation since my app plays some videos and they dont look right if the screen is not rotated – Rodolfo Nov 23 '16 at 04:44

1 Answers1

1

If you look at the AndroidManifest.xml from the link you gave you will see that <activity> has property android:configChanges="orientation|keyboardHidden".
So you need to do the same for your activity in the manifest with small difference: android:configChanges="orientation|screenSize|keyboardHidden".

Here's a full example of Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ca.ruffarc.flytest">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity android:name=".MainActivity"
                  android:configChanges="orientation|screenSize|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
Community
  • 1
  • 1
makvasic
  • 213
  • 1
  • 8
  • thank you for your suggestion. Everything compiles fine, but it still does the same thing. I rotate the screen and it refreshes. It now looks like: android:configChanges="orientation|screenSize|keyboardHidden" – Rodolfo Nov 22 '16 at 15:55
  • problem is that confingChanges need to be inside activity tag. – makvasic Nov 23 '16 at 08:17
  • Please see [edited answer](http://stackoverflow.com/a/40746138/7177329) if this wasn't clear... – makvasic Nov 23 '16 at 08:26