mirror of
https://github.com/flutter/samples.git
synced 2026-04-20 22:14:05 +00:00
Adds data transfer to Add2App samples, using caching on Android (#146)
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
package="dev.flutter.example.androidfullscreen">
|
||||
|
||||
<application
|
||||
android:name="io.flutter.app.FlutterApplication"
|
||||
android:name=".MyApplication"
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
|
||||
@@ -6,18 +6,32 @@ package dev.flutter.example.androidfullscreen
|
||||
|
||||
import android.os.Bundle
|
||||
import android.widget.Button
|
||||
import android.widget.TextView
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
private var counterLabel: TextView? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
counterLabel = findViewById(R.id.counter_label)
|
||||
|
||||
val button = findViewById<Button>(R.id.launch_button)
|
||||
|
||||
button.setOnClickListener {
|
||||
startActivity(FlutterActivity.createDefaultIntent(this))
|
||||
val intent = FlutterActivity
|
||||
.withCachedEngine(ENGINE_ID)
|
||||
.build(this)
|
||||
startActivity(intent)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
val app = application as MyApplication
|
||||
counterLabel?.text = "Current count: ${app.count}"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
// Copyright 2019 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
package dev.flutter.example.androidfullscreen
|
||||
|
||||
import android.app.Application
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.embedding.engine.FlutterEngineCache
|
||||
import io.flutter.embedding.engine.dart.DartExecutor
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import io.flutter.view.FlutterMain
|
||||
|
||||
const val ENGINE_ID = "1"
|
||||
|
||||
class MyApplication : Application() {
|
||||
var count = 0
|
||||
|
||||
private var channel: MethodChannel? = null
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
FlutterMain.startInitialization(this)
|
||||
FlutterMain.ensureInitializationComplete(this, null)
|
||||
|
||||
val flutterEngine = FlutterEngine(this)
|
||||
flutterEngine
|
||||
.dartExecutor
|
||||
.executeDartEntrypoint(
|
||||
DartExecutor.DartEntrypoint.createDefault()
|
||||
)
|
||||
|
||||
FlutterEngineCache.getInstance().put(ENGINE_ID, flutterEngine)
|
||||
|
||||
channel = MethodChannel(flutterEngine.dartExecutor, "dev.flutter.example/counter")
|
||||
|
||||
channel?.setMethodCallHandler { call, _ ->
|
||||
when (call.method) {
|
||||
"incrementCounter" -> {
|
||||
count++
|
||||
reportCounter()
|
||||
}
|
||||
"requestCounter" -> {
|
||||
print("requestCounter")
|
||||
reportCounter()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun reportCounter() {
|
||||
channel?.invokeMethod("reportCounter", count)
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,18 @@
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/counter_label"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="32dp"
|
||||
android:text="Current counter: 0"
|
||||
app:layout_constraintBottom_toTopOf="@+id/launch_button"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
Reference in New Issue
Block a user