Android-阻止活动停留在后台后重新启动

时间:2018-11-29 06:44:58

标签: android android-activity android-library

我正在开发一个包含4个活动[A,B,C,D]的图书馆

Activity A is launched by user-app
    A launches B and expect response on `onActivityResult`.
        B finishes by setting RESULT_OK
    A launches C and expect response on `onActivityResult`.
        C do some Image processing on a 4096x2160 image and finishes by setting RESULT_OK
    A launches D and expect response on `onActivityResult`.
        D finishes by setting RESULT_OK
    A set it's result as  RESULT_OK finishes.
The result is returned to the user-app.

此步骤后出现问题 C do some Image processing on a 4096x2160 image and finishes by setting RESULT_OK

在此步骤之后,活动A从后面被销毁,并且A中的onCreate被调用,即再次调用A launches B and expect response on onActivityResult.。我怎样才能更好地做到这一点,以免出现意外关机。

这是活动性代码A:

class  SelectPackage : AppCompatActivity() {

companion object {

    internal const val RES_PACKAGE_CANCEL = 100
    internal const val RES_PACKAGE_OK = 104
    internal const val RES_EDITACTIVITY_CANCEL = 101
    internal const val RES_EDITACTIVITY_OK = 105
    internal const val RES_SUMMARY_CANCEL = 102
    internal const val RES_SUMMARY_OK = 103
}

private val REQ_CODE = 1023

private val data: Data by lazy { makeDataObject() }

private fun makeDataObject(): Data {
    val data = Data()

    data.clientToken = intent.getStringExtra(clientToken)
    ....
    ....
    data.screenSize = intent.getStringExtra(screenSize)

    return data
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_select_package)

    Log.d("YYY", "Activity started again")
    val i = Intent(this, Packages::class.java)
    i.putExtra(SelectPackage.DATA, data)

    startActivityForResult(i, REQ_CODE)

}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == REQ_CODE) {
        when (resultCode) {

            RES_PACKAGE_CANCEL -> {
                setResult(AppCompatActivity.RESULT_CANCELED)
                finish()
            }

            RES_PACKAGE_OK -> {
                if (data != null) {
                    val intent = Intent(this, EditActivity::class.java)
                    intent.putExtra(SelectPackage.DATA, data.getSerializableExtra(SelectPackage.DATA))
                    intent.putExtra(Pkgs.TAG, data.getSerializableExtra(Pkgs.TAG))
                    startActivityForResult(intent, REQ_CODE)
                }
            }

            RES_EDITACTIVITY_CANCEL -> {
                setResult(AppCompatActivity.RESULT_CANCELED)
                finish()
            }

            RES_EDITACTIVITY_OK -> {
                if (data != null) {
                    val intent = Intent(this, SummaryActivity::class.java)
                    intent.putExtra(SelectPackage.DATA, data.getSerializableExtra(SelectPackage.DATA))
                    intent.putExtra(Pkgs.TAG, data.getSerializableExtra(Pkgs.TAG))
                    startActivityForResult(intent, REQ_CODE)
                }
            }

            RES_SUMMARY_CANCEL -> {
                setResult(AppCompatActivity.RESULT_CANCELED)
                finish()
            }

            RES_SUMMARY_OK -> {
                if (data != null) {
                    val intent = Intent()
                    intent.putExtra(SelectPackage.packageName, data.getStringExtra(SelectPackage.packageName))
                    intent.putExtra(SelectPackage.packageId, data.getStringExtra(SelectPackage.packageId))
                    intent.putExtra(SelectPackage.packagePrice, data.getFloatExtra(SelectPackage.packagePrice,0f))
                    setResult(AppCompatActivity.RESULT_OK)
                    finish()
                }
            }
        }

    }
}

}

1 个答案:

答案 0 :(得分:1)

RAM不足会导致操作系统终止您的活动A的进程(请查看this)。也许您在活动C的过程中做得太多。

一种解决方法是将结果存储到本地存储中(例如:SharedPreferences)。在活动A的onCreate()中,检查本地存储以决定下一步要做什么。