Advertisement
MateuszGrabarczyk

kotlin kartka

Jan 20th, 2023
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. package pl.polsl.lab6v2022
  2.  
  3. import android.content.pm.ActivityInfo
  4. import androidx.appcompat.app.AppCompatActivity
  5. import android.os.Bundle
  6. import android.webkit.JavascriptInterface
  7. import android.webkit.WebView
  8.  
  9. class Kartka : AppCompatActivity() {
  10.  
  11. var myString: String = ""
  12.  
  13. override fun onCreate(savedInstanceState: Bundle?) {
  14. super.onCreate(savedInstanceState)
  15.  
  16. //WebView - kontrolka wyswietlajaca html
  17. val page = WebView(this)
  18.  
  19. if (savedInstanceState != null) {
  20. myString = savedInstanceState.getString("giftsList", "")
  21. } else {
  22. myString = intent.getStringExtra("giftsList")!!
  23. }
  24.  
  25. //wlaczenie obslugi JS
  26. page.settings.javaScriptEnabled=true
  27.  
  28. //dodanie interfejsu pomiędzy Kotlinem a JS
  29. //this - obiekt tej klasy dostarcza metody JSInterface, activity - nazwa widoczna w JS
  30. page.addJavascriptInterface(this, "activity") //ODKOMENTOWAC DLA JS
  31.  
  32. //zaladowanie zawartosci kontroli WebView - pliki z katalogu assests w projekcie
  33. page.loadUrl("file:///android_asset/Kartka.html")
  34.  
  35. this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
  36.  
  37. //wstawienie kontrolki WebView jako calej fasady aktywnosci
  38. setContentView(page)
  39.  
  40.  
  41. }
  42.  
  43. override fun onSaveInstanceState(outState: Bundle) {
  44. super.onSaveInstanceState(outState)
  45. outState.putString("giftsList", myString)
  46. }
  47.  
  48. @JavascriptInterface
  49. fun getGifts(): String {
  50. return myString
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement