Search code examples
androidandroid-activity

How to handle the ActivityNotFoundException?


In my app , I need to use startActivity to see the content of the file , or use the default application to open the certain file , but sometimes the android system may not install the application which is needed .

My question is how to handle this exception . I want a toast , not FC..

Any Advice? THX


Solution

  • Just simply add that activity in your manifest file..

    like,

    <activity android:name=".ActivityName"
                      android:label="@string/app_name">
            </activity>
    

    EDIT:

    Now to catch the ActivityNOtFoundException put your code in,

    try {
    
      // Your startActivity code wich throws exception  
    } catch (ActivityNotFoundException activityNotFound) {
    
        // Now, You can catch the exception here and do what you want
    }
    

    Note: Be careful when you catch this ActivityNotFound Exception but you can't modified manifest file to run time, means once you encountered the exception and if you want to add that this activity tag at runtime then you can't.