You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

触发SensorEventListener后无法启动GetInTouchActivity及Fragment问题排查

问题:摇晃手机时GetInTouchActivity的onCreate未执行,Fragment无法显示

我正在使用SensorEventListener,希望在检测到手机摇晃时,通过GetInTouchActivity打开GetInTouchMenuFragment。但GetInTouchActivity的onCreate方法完全未执行,导致Fragment无法显示。

MainActivity 相关代码

override fun onCreate(savedInstanceState: Bundle?) {
    // Getting the Sensor Manager instance
    sensorManager = this.getSystemService(Context.SENSOR_SERVICE) as SensorManager

    Objects.requireNonNull(sensorManager)!!
        .registerListener(sensorListener, sensorManager!!
            .getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL)

}

private val sensorListener: SensorEventListener = object : SensorEventListener {
    override fun onSensorChanged(event: SensorEvent) {

        // Fetching x,y,z values
        val x = event.values[0]
        val y = event.values[1]
        val z = event.values[2]
        lastAcceleration = currentAcceleration

        // Getting current accelerations
        // with the help of fetched x,y,z values
        currentAcceleration = sqrt((x * x + y * y + z * z).toDouble()).toFloat()
        val delta: Float = currentAcceleration - lastAcceleration
        acceleration = acceleration * 0.9f + delta

        // Display a Toast message if
        // acceleration value is over 12
        if (acceleration > 12) {
            Log.d("GetInTouchActivity", "first")
            GetInTouchActivity.createIntent(this@MainActivity)
            Log.d("GetInTouchActivity", "second")
        }
    }
    override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
}

GetInTouchActivity 相关代码

class GetInTouchActivity : InjectionFragmentActivity() {

    @Inject
    lateinit var viewModelFactory: ViewModelProvider.Factory

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

        setAppBar(toolbar, showHomeAsUp = true, navIcon = AppBarNavIcon.BACK_ARROW_BLACK)

        if (savedInstanceState == null) {
            Log.d("GetInTouchActivity", "onCreate") // NOT EXECUTED
            showFragment(GetInTouchMenuFragment.newInstance())
        }
    }

    override fun onBackPressed() {
        reporter.reportEvent(javaClass.simpleName.processScreenName(), "Back")

        finish()
    }

    companion object {
        fun createIntent(context: Context): Intent {
            Log.d("GetInTouchActivity", "createIntent") // EXECUTED
            return Intent(context, GetInTouchActivity::class.java)
        }
    }
}

解决方案

核心问题

你只创建了启动GetInTouchActivity的Intent对象,但没有调用启动Activity的方法,所以Activity根本没有被启动,自然不会执行onCreate方法。

在MainActivity的onSensorChanged方法中,GetInTouchActivity.createIntent(this@MainActivity)仅返回一个Intent实例,并未触发Activity的启动流程。

修复代码

修改MainActivity中摇晃检测后的代码,添加startActivity调用:

if (acceleration > 12) {
    Log.d("GetInTouchActivity", "first")
    val intent = GetInTouchActivity.createIntent(this@MainActivity)
    startActivity(intent) // 关键:添加这一行启动Activity
    Log.d("GetInTouchActivity", "second")
}

额外注意事项

  • 确保GetInTouchActivity已在AndroidManifest.xml中注册,否则会抛出ActivityNotFoundException:
    <activity android:name=".GetInTouchActivity" />
    
  • 若要避免重复启动Activity(比如当前Activity已在栈顶),可添加启动标记:
    val intent = GetInTouchActivity.createIntent(this@MainActivity)
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
    startActivity(intent)
    

内容的提问来源于stack exchange,提问作者BRDroid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 08:55:20