Search code examples
androidandroid-studioandroid-intentintentfilterandroid-implicit-intent

Why does the Main activity has an intent filter?


  1. If the intent filters are to resolve implicit intents, then why does the MainActivity(which is the very first activity that is run when app is launched) has an intent filter?
  2. Who send an implicit intent to it?
  3. What if the sent implicit intent doesn't have proper data ?

Solution

    1. It has CATEGORY_LAUNCHER and ACTION_MAIN . android.intent.action.MAIN means that this activity is the entry point of the application, i.e. when you launch the application, this activity is created. CATEGORY_LAUNCHER tells that your activity should be displayed in the top-level launcher.

    https://developer.android.com/reference/android/content/Intent.html#ACTION_MAIN

    1. Launcher sends implicit intent to it. This is how launcher knows which activity is to be opened on click.

    2. If you send improper data it will not open your activity. For ex: If you try to start your main activity using implicit intent only in startActivity then it will not start because there is CATEGORY_DEFAULT associated with it. You need to add one more intent_filter to your activity to resolve the intents.