Android language switching LOCALE_CHANGED with CalendarView, kotlin

Android language switching LOCALE_CHANGED with CalendarView, kotlin

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.CalendarView
import androidx.appcompat.app.AppCompatActivity
import java.util.Locale

class MainActivity : AppCompatActivity() {
    private val TAG = "fly"
    private var mChangeReceiver: ChangeReceiver? = null
    private var mCalendarView: CalendarView? = null

    companion object {
        var isRestart = false
        var mDate: Long? = -1L
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "---")
        Log.d(TAG, "onCreate")

        setContentView(R.layout.activity_main)
        mChangeReceiver = ChangeReceiver(TAG)

        val intentFilter = IntentFilter()
        intentFilter.addAction(Intent.ACTION_LOCALE_CHANGED)
        registerReceiver(mChangeReceiver, intentFilter)

        mCalendarView = findViewById(R.id.calendar_view)
        mCalendarView?.setOnDateChangeListener(object : CalendarView.OnDateChangeListener {
            override fun onSelectedDayChange(
                view: CalendarView,
                year: Int,
                month: Int,
                dayOfMonth: Int
            ) {
                Log.d(TAG, "$year-$month-$dayOfMonth ${mCalendarView?.date}")
                mDate = mCalendarView?.date
            }
        })

        if (isRestart) {
            Log.d(TAG, "重启,设置历史选择 $mDate")
            mDate?.let { mCalendarView?.setDate(it, true, true) }
            isRestart = false
        }
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume $mDate")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy")
        unregisterReceiver(mChangeReceiver)
    }

    private class ChangeReceiver(val tag: String) : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            var action = intent?.action
            if (!TextUtils.isEmpty(action)) {
                if (action == Intent.ACTION_LOCALE_CHANGED) {
                    var locale = Locale.getDefault()
                    Log.d(
                        tag,
                        "${locale.country}-${locale.language}"
                    )

                    isRestart = true
                }
            }
        }
    }
}

If you want to prevent the Activity from restarting after the system switches the language setting, set it for the current Activity in the configuration file of AndroidManifest.xml:

android:configChanges="locale|layoutDirection|keyboard"

After this setting, after reopening the activity, the activity will not restart (created from onCreate), but will go through the onResume cycle.

Android rewrites onConfigurationChanged to avoid re-entering the onCreate life cycle when switching between horizontal and vertical screens , it will be forced to re-enter the activity life cycle onCreate. In some special scenarios, the developer does not want to re-enter the onCreate life cycle. Then, it can be avoided by rewriting Android's onConfigurationChanged. https://blog.csdn.net/zhangphil/article/details/50607742

Guess you like

Origin blog.csdn.net/zhangphil/article/details/129915107