0

my goal is to make my Password Manager app's current visible activity (e.g. MainMenuActivity) change to EmptyScreenActivity when app is paused (so nobody can see the passwords when the app is in background) and then run LoginScreenActivity when the app is resumed for user to log back in. I used this code by vokilam to implement LifecycleObserver and it works pretty well but I'm stuck because there is no option to pass the Intent to the AppLifecycleListener class. How can I overcome this issue? Thank you in advance.

AppLifecycleListener.kt


import android.app.Application
import android.util.Log
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ProcessLifecycleOwner

class App : Application() {

    override fun onCreate() {
        super.onCreate()
        ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifecycleListener())
    }
}

class AppLifecycleListener : DefaultLifecycleObserver {

    override fun onResume(owner: LifecycleOwner) {
        Log.i(null, "Resume")
    }

    override fun onStop(owner: LifecycleOwner) {
        Log.i(null, "Stop")
    }
}

MainMenuActivity.kt

class MainMenuActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_menu)

        supportActionBar?.hide()

        val goToPassGeneratorScreenButton = findViewById<Button>(R.id.goToPassGeneratorScreenButton)
        val goToPassCheckScreenButton = findViewById<Button>(R.id.goToPassCheckScreenButton)
        val addServicePasswordButton = findViewById<Button>(R.id.addServicePasswordButton)
        val updateServicePasswordButton = findViewById<Button>(R.id.updateServicePasswordButton)
        val updateMasterkKeyButton = findViewById<Button>(R.id.updateMasterKeyButton)

        goToPassGeneratorScreenButton.setOnClickListener {
            val intent = Intent(this, PassGeneratorActivity::class.java)
            startActivity(intent)
        }
        goToPassCheckScreenButton.setOnClickListener {
            val intent = Intent(this, PassViewerActivity::class.java)
            startActivity(intent)
        }

        addServicePasswordButton.setOnClickListener {
            val intent = Intent(this, PassCreatorActivity::class.java)
            startActivity(intent)
        }

        updateServicePasswordButton.setOnClickListener {
            val intent = Intent(this, PassEditorActivity::class.java)
            startActivity(intent)
        }

        updateMasterkKeyButton.setOnClickListener {
            val intent = Intent(this, MasterKeyEditorActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onBackPressed() {
        moveTaskToBack(true)
    }

fragment of AndroidManifest.xml

...
    <application
        android:name=".App"
...
    </application>

  • You cannot start an activity from the background on modern versions of Android. – CommonsWare Jun 29 '22 at 19:42
  • You could use a LocalBroadcastManager to let the Activity know about the background/foreground and hide/show the content accordingly. – Darshan Jun 29 '22 at 20:03

0 Answers0