Core Android Framework, Systems

By pjain      Published Dec. 16, 2019, 6:04 a.m. in blog Programming   

Activity, Core Framework 101

Application

Activity Lifecycle, Actions in Activity

onCreate() {
    override fun onBackPressed() {
    }
}

Activity Core with Data Binding "Synthetic" in Kotlin, lateinit, lazy

  • PKJQ ?is the synthetic.main.* requrired?

  • Code in MobAndrw/kotlin/

    import android.support.v7.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_main. import kotlinx.android.synthetic.main.layout_popup.

    class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //<-- data bound by kotlin synthetic -> main/ res/layout/ activity_main.xml btn_show_popup.setOnClickListener { // put onclick action here }

    // activity_main.xml has a button
    constraintLayout &gt; &lt;Button id:btn_show_popup text="show popup"
    }
    

    }

Intents

Simple Intents, Data Passing and ActivityResult

Simple Intents

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android" android:padding="25dp"
    android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt;
    &lt;Button android:id="@+id/btn_explicit"    android:text="Explicit Intent"
        android:layout_width="match_parent"   android:layout_height="wrap_content" /&gt;
    &lt;Button android:id="@+id/btn_implicit"    android:text="Implicit Intent"
        android:layout_width="match_parent"   android:layout_height="wrap_content" /&gt;
&lt;/LinearLayout&gt;

import android.content.Intent
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
  onCreate ..
    val context = this
    btn_explicit.setOnClickListener {  // weird example - relaunch THIS activity after closing this one!
        val intent = Intent(context,MainActivity::class.java)
        startActivity(intent)
        finish()
    }
    btn_implicit.setOnClickListener { // Let user pick which to share with eg email .. 
        var intent = Intent(Intent.ACTION_SEND)
        intent.putExtra(Intent.EXTRA_TEXT,"CodeAndroid")
        intent.type ="text/plain"
        startActivity(intent)
    }

Data Passing

r Data Pass

ActivityResult

//ActivityResultEx - ux &lt;btn_read open file&gt; _______ tv_read_data read data TV _______
~wmkt/ActivityResultExample
  • Manifest needs

    const val FILE_PICKER_ID = 12 lateinit var context : Context onCreate {.. context = this btn_read.setOnClickListener({ var intent = Intent(Intent.ACTION_GET_CONTENT) intent.type = "text/*" //set mime type of file for picker .. user will choose .. startActivityForResult(intent,FILE_PICKER_ID) }) } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if(requestCode == FILE_PICKER_ID && resultCode == Activity.RESULT_OK){ val dest = File(PathUtils.getPath(context,data!!.data)) val readData = FileInputStream(dest).bufferedReader().use { it.readText() } tv_read_data.text = readData } }

Packaged Intent - EM SMS

Form ET to Send HTML Email

  • uses synthetic binding to get form edit text, submit sends it ..
    val i = Intent(Intent.ACTION_SEND)
    i.type = "text/html"
    i.putExtra(iIntent.EXTRA_EMAIL,arrayOf("sendto@email.com"))  // or arrayOf&lt;String&gt;(...) 
    i.putExtra(iIntent.EXTRA_CC,   arrayOf("CC@email.com"))
    i.putExtra(iIntent.EXTRA_BCC,  arrayOf("BCC@email.com"))
    i.putExtra(iIntent.EXTRA_SUBJECT,"RE: asdf subject")
    i.putExtra(iIntent.EXTRA_TEXT, arrayOf("html email body"))
    startActivity(i)
    

AsyncTask, Job

// ~wmkt/DemoAsyncTask

Services

BroadcastReceivers and Notifications

PendingIntents

IntentService

// ~wmkt/IntentServiceExample

Pending Intent, Service Intent

Notification

// ~wmkt/NotificationExample

Alarms Manager

Timer, Clock

Make a Timer App: Countdown Timer in Kotlin (Ep 2 of 3)

Alarm Manager, DB

~wmkt/AlarmManagerExample

Reminders, Alarms, Timers

// ~wmkt/ https://github.com/ResoCoder/TimerAppAndroidTutorial.git


0 comments

There are no comments yet

Add new comment

Similar posts

Serverless Computing

Android Layouts Design 101

Drawing, Canvas in Kotlin

Custom Views