<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=Mobile_Apps_2023W_Lecture_21</id>
	<title>Mobile Apps 2023W Lecture 21 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=Mobile_Apps_2023W_Lecture_21"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Mobile_Apps_2023W_Lecture_21&amp;action=history"/>
	<updated>2026-04-08T03:33:34Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=Mobile_Apps_2023W_Lecture_21&amp;diff=24416&amp;oldid=prev</id>
		<title>Soma: Created page with &quot;==Notes==  &lt;pre&gt; March 29 --------  So I&#039;m trying to figure out how to implement the back button properly.  Currently, the back button gets triggered but doesn&#039;t work right, .goBack() isn&#039;t doing what we expected  - hypothesis: state changes are updating history too often, getting copies of current page and so can&#039;t keep track of last page properly  Two choices:  - integrate more tightly with webview history so we can better manipulate and interact with it  - ignore webv...&quot;</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=Mobile_Apps_2023W_Lecture_21&amp;diff=24416&amp;oldid=prev"/>
		<updated>2023-03-30T03:26:48Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;==Notes==  &amp;lt;pre&amp;gt; March 29 --------  So I&amp;#039;m trying to figure out how to implement the back button properly.  Currently, the back button gets triggered but doesn&amp;#039;t work right, .goBack() isn&amp;#039;t doing what we expected  - hypothesis: state changes are updating history too often, getting copies of current page and so can&amp;#039;t keep track of last page properly  Two choices:  - integrate more tightly with webview history so we can better manipulate and interact with it  - ignore webv...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Notes==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
March 29&lt;br /&gt;
--------&lt;br /&gt;
&lt;br /&gt;
So I&amp;#039;m trying to figure out how to implement the back button properly.&lt;br /&gt;
&lt;br /&gt;
Currently, the back button gets triggered but doesn&amp;#039;t work right, .goBack() isn&amp;#039;t doing what we expected&lt;br /&gt;
 - hypothesis: state changes are updating history too often, getting copies of current page and so can&amp;#039;t keep track of last page properly&lt;br /&gt;
&lt;br /&gt;
Two choices:&lt;br /&gt;
 - integrate more tightly with webview history so we can better manipulate and interact with it&lt;br /&gt;
 - ignore webview history, implement it all on our own and just push current page to the webview widget as needed&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;kotlin&amp;quot; line&amp;gt;&lt;br /&gt;
package carleton.comp2601.webviewcompose&lt;br /&gt;
&lt;br /&gt;
import android.os.Bundle&lt;br /&gt;
import android.util.Log&lt;br /&gt;
import android.view.ViewGroup&lt;br /&gt;
import android.webkit.WebView&lt;br /&gt;
import android.webkit.WebViewClient&lt;br /&gt;
import androidx.activity.ComponentActivity&lt;br /&gt;
import androidx.activity.compose.setContent&lt;br /&gt;
import androidx.compose.foundation.layout.Row&lt;br /&gt;
import androidx.compose.foundation.text.ClickableText&lt;br /&gt;
import androidx.compose.material.*&lt;br /&gt;
import androidx.compose.runtime.*&lt;br /&gt;
import androidx.compose.ui.graphics.Color&lt;br /&gt;
import androidx.compose.ui.text.AnnotatedString&lt;br /&gt;
import androidx.compose.ui.tooling.preview.Preview&lt;br /&gt;
import androidx.compose.ui.viewinterop.AndroidView&lt;br /&gt;
&lt;br /&gt;
class MainActivity : ComponentActivity() {&lt;br /&gt;
&lt;br /&gt;
    override fun onCreate(savedInstanceState: Bundle?) {&lt;br /&gt;
        super.onCreate(savedInstanceState)&lt;br /&gt;
        setContent {&lt;br /&gt;
            // Calling the composable function&lt;br /&gt;
            // to display element and its contents&lt;br /&gt;
            MainContent()&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Creating a composable&lt;br /&gt;
// function to display Top Bar&lt;br /&gt;
@Composable&lt;br /&gt;
fun MainContent() {&lt;br /&gt;
    var curURL by remember { mutableStateOf(&amp;quot;https://news.ycombinator.com&amp;quot;) }&lt;br /&gt;
    var navigate by remember { mutableStateOf(&amp;quot;&amp;quot;) }&lt;br /&gt;
&lt;br /&gt;
    Scaffold(&lt;br /&gt;
//        topBar = { TopAppBar(title = {Text(&amp;quot;COMP 2601&amp;quot;)})},&lt;br /&gt;
        topBar = {&lt;br /&gt;
            Row {&lt;br /&gt;
                ClickableText(&lt;br /&gt;
                    AnnotatedString(text = &amp;quot;Back&amp;quot;),&lt;br /&gt;
                    onClick = {&lt;br /&gt;
                        navigate = &amp;quot;back&amp;quot;&lt;br /&gt;
                        Log.d(&amp;quot;webviewCompose&amp;quot;, &amp;quot;navigate = back&amp;quot;)&lt;br /&gt;
                    }&lt;br /&gt;
                )&lt;br /&gt;
                URLBar(curURL = curURL, updateURL = { curURL = it })&lt;br /&gt;
            }&lt;br /&gt;
        },&lt;br /&gt;
        content = { MyContent(curURL = curURL, updateURL = {&lt;br /&gt;
            curURL = it&lt;br /&gt;
            Log.d(&amp;quot;webviewCompose&amp;quot;, &amp;quot;updating URL to $curURL&amp;quot;)&lt;br /&gt;
                                                           },&lt;br /&gt;
            navigate = navigate, resetNavigate = { navigate = &amp;quot;&amp;quot;}) }&lt;br /&gt;
    )&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
@Composable&lt;br /&gt;
fun URLBar(curURL: String, updateURL: (String) -&amp;gt; Unit) {&lt;br /&gt;
    var newURL = &amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    TextField(&lt;br /&gt;
        value = curURL,&lt;br /&gt;
        //onFocusChange = { updateURL(newURL) },&lt;br /&gt;
        //onValueChange = { updatedURL: String -&amp;gt; newURL = updatedURL},&lt;br /&gt;
        onValueChange = updateURL,&lt;br /&gt;
        label = { Text(&amp;quot;Enter URL here&amp;quot;) }&lt;br /&gt;
    )&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
@Composable&lt;br /&gt;
fun MyContent(curURL: String, updateURL: (String) -&amp;gt; Unit,&lt;br /&gt;
              navigate: String, resetNavigate: () -&amp;gt; Unit){&lt;br /&gt;
&lt;br /&gt;
    class ObservableWebViewClient : WebViewClient() {&lt;br /&gt;
&lt;br /&gt;
        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {&lt;br /&gt;
            if (url != null) {&lt;br /&gt;
                updateURL(url)&lt;br /&gt;
            }&lt;br /&gt;
            return false&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    fun logWebViewHistory(view: WebView) {&lt;br /&gt;
        val history = view.copyBackForwardList()&lt;br /&gt;
&lt;br /&gt;
        val current = history.currentIndex&lt;br /&gt;
&lt;br /&gt;
        Log.d(&amp;quot;webviewCompose&amp;quot;, &amp;quot;Current history item: $current&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
        Log.d(&amp;quot;webviewCompose&amp;quot;, &amp;quot;Webview BackForwardList contents:&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
        for (i in 0..history.size -1) {&lt;br /&gt;
            val item = history.getItemAtIndex(i)&lt;br /&gt;
&lt;br /&gt;
            if (item != null) {&lt;br /&gt;
                val u = item.originalUrl&lt;br /&gt;
                Log.d(&amp;quot;webviewCompose&amp;quot;, &amp;quot;  $i: $u&amp;quot;)&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Adding a WebView inside AndroidView&lt;br /&gt;
    // with layout as full screen&lt;br /&gt;
    AndroidView(factory = {&lt;br /&gt;
        WebView(it).apply {&lt;br /&gt;
//            layoutParams = ViewGroup.LayoutParams(&lt;br /&gt;
//                ViewGroup.LayoutParams.MATCH_PARENT,&lt;br /&gt;
//                ViewGroup.LayoutParams.MATCH_PARENT&lt;br /&gt;
//            )&lt;br /&gt;
            webViewClient = ObservableWebViewClient()&lt;br /&gt;
            loadUrl(curURL)&lt;br /&gt;
        }&lt;br /&gt;
    }, update = {&lt;br /&gt;
        if (navigate == &amp;quot;back&amp;quot;) {&lt;br /&gt;
            Log.d(&amp;quot;webviewCompose&amp;quot;, &amp;quot;Navigating back&amp;quot;)&lt;br /&gt;
            val currentURL = it.url&lt;br /&gt;
            Log.d(&amp;quot;webviewCompose&amp;quot;, &amp;quot;currentURL = $currentURL&amp;quot;)&lt;br /&gt;
            it.goBack()&lt;br /&gt;
            val prevURL = it.url&lt;br /&gt;
            Log.d(&amp;quot;webviewCompose&amp;quot;, &amp;quot;prevURL = $prevURL&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
            if (currentURL == prevURL) {&lt;br /&gt;
                Log.d(&amp;quot;webviewCompose&amp;quot;, &amp;quot;current and prev are the SAME!&amp;quot;)&lt;br /&gt;
            }&lt;br /&gt;
            if (prevURL != null) {&lt;br /&gt;
                Log.d(&amp;quot;webviewCompose&amp;quot;, &amp;quot;updating URL after back button&amp;quot;)&lt;br /&gt;
                updateURL(prevURL)&lt;br /&gt;
            }&lt;br /&gt;
            resetNavigate()&lt;br /&gt;
&lt;br /&gt;
            logWebViewHistory(it)&lt;br /&gt;
        } else {&lt;br /&gt;
            Log.d(&amp;quot;webviewCompose&amp;quot;, &amp;quot;loading URL $curURL&amp;quot;)&lt;br /&gt;
            it.loadUrl(curURL)&lt;br /&gt;
            logWebViewHistory(it)&lt;br /&gt;
        }&lt;br /&gt;
    })&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// For displaying preview in&lt;br /&gt;
// the Android Studio IDE emulator&lt;br /&gt;
@Preview(showBackground = true)&lt;br /&gt;
@Composable&lt;br /&gt;
fun DefaultPreview() {&lt;br /&gt;
    MainContent()&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Soma</name></author>
	</entry>
</feed>