Launching default android launcher programmatically

Viewed 25363

I'm looking for a way to launch the default android launcher programatically, something perhaps like the code below. Or do I have to add something to the manifest file? Thanks!

Intent intent = new Intent();
intent.setClassName("com.android.launcher", "Launcher");
startActivity(intent);
4 Answers
=> In kotlin add below code in onDestroy method of appCompactActvity use to make your app as default launcher, 

override fun onDestroy() {
        var intent = Intent(Intent.ACTION_MAIN)
        var packageManager: PackageManager = packageManager
        for (resolveInfo in packageManager.queryIntentActivities(Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), PackageManager.MATCH_DEFAULT_ONLY)) {
            if (packageName != resolveInfo.activityInfo.packageName)  //if this activity is not in our activity (in other words, it's another default home screen)
            {
                startActivity(intent)
            }
            break
        }
        super.onDestroy()
    }
 startActivity( Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));
Related