LaurenceGough
LaurenceGough

Reputation: 13

How to program an application on launch to close and automatically open the home screen (Launcher) (Wear OS)

I am a beginner to programming in general and never have I programmed in Android Studio so I am probably doing all sorts wrong.

I just wanted to make a very simple app for my WearOS 3 (Based on Android 11) watch.

On launch of the application from any area (the apk) I want it to open the home screen (default Launcher). That's it. It doesn't need a UI at all.

Here is what I have so far but it gets stuck in a loop I believe. The program runs but then crashes, debug gives no information as to the reason of the crash:

package com.example.returntohome

import android.app.Activity
import android.content.Intent
import android.os.Bundle


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val startMain = Intent(Intent.ACTION_MAIN)
        startMain.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        startMain.addCategory(Intent.CATEGORY_HOME)
        moveTaskToBack(true)

    }
}

I am using Kotlin. What can I try to solve this?

Upvotes: 1

Views: 302

Answers (1)

Dan Harms
Dan Harms

Reputation: 4840

Your intent is correct. You are just missing the last bit to actually initiate the intent.

val startMain = Intent(Intent.ACTION_MAIN)
startMain.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startMain.addCategory(Intent.CATEGORY_HOME)
startActivity(startMain) // <- Start the intent

Upvotes: 1

Related Questions