Search code examples
androidandroid-7.0-nougatandroid-7.1-nougatandroid-appshortcut

activity must not be interdependant on any other activities for app shortcuts?


I am implementing app shortcuts in my application.When i tried to open my shortcut it opened fine but during a search functionality it crashed the crash report showed an SQLiteexception cause i was fetching data from DB So my question is should app shortcuts be independant of other files?

    01-28 15:25:51.784 12166-12166/ E/AndroidRuntime: FATAL EXCEPTION: main
                                                          Process: , PID: 12166
                                                          android.database.sqlite.SQLiteException: no such table: temp (code 1): , while compiling: SELECT empID, name, firstName, lastName, email, mobile, designation, location, extension FROM temp WHERE name LIKE ?
                                                              at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                              at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                              at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                              at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                              at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                              at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                                                              at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                                                              at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
                                                              at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1165)
                                                              at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1036)
                                                              at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1204)
                                                              at com.thbs.database.EmployeeDbAdapter.search(EmployeeDbAdapter.java:194)
                                                              at com.thbs.activity.SearchActivity$3.onTextChanged(SearchActivity.java:306)
                                                              at android.widget.TextView.sendOnTextChanged(TextView.java:8187)
                                                              at android.widget.TextView.handleTextChanged(TextView.java:8249)
                                                              at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10371)
                                                              at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1208)
                                                              at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:578)
                                                              at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:509)
                                                              at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:508)
                                                              at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:844)
                                                              at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:198)
                                                              at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
                                                              at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:353)
                                                              at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:93)
                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                              at android.os.Looper.loop(Looper.java:154)
                                                              at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

it works fine when opened through the application

my code for creating app shortcut is

if (Build.VERSION.SDK_INT >= 25) {
        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
        ShortcutInfo leaveShortcut = new ShortcutInfo.Builder(this, "shortcut_leave")
                .setShortLabel("Leave Manager")
                .setLongLabel("Leave Manager")
                .setIcon(Icon.createWithResource(this, R.drawable.app_icon))
                .setIntents(
                        new Intent[]{
                                new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, LeaveManagerActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
                        })
                .build();
        ShortcutInfo searchShortcut = new ShortcutInfo.Builder(this, "shortcut_search")
                .setShortLabel("Employee search")
                .setLongLabel("Employee search")
                .setIcon(Icon.createWithResource(this, R.drawable.app_icon))
                .setIntents(
                        new Intent[]{
                                new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, SearchActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
                        })
                .build();


        shortcutManager.setDynamicShortcuts(Arrays.asList(leaveShortcut, searchShortcut));
    }

Solution

  • Most likely, your problem has nothing to do with app shortcuts. As an experiment, add the <intent-filter> for MAIN/LAUNCHER to your SearchActivity, so your app now has two launcher icons. Launch SearchActivity directly from the home screen. Most likely, you will have the same crash.

    My guess is that SearchActivity — and perhaps LeaveManagerActivity — assume that the user always goes through whatever your launcher activity is, such that your database is in a certain state. That will not always be the case. Not only might app shortcuts cause this, but other things will too. For example:

    • User enters your app through the launcher activity
    • User does something to go to SearchActivity
    • User presses HOME
    • 10 minutes pass, during which time Android terminates your process to free up system RAM
    • User returns to your app (home screen launcher icon, overview screen/recent-tasks list, etc.)

    At this point, Android will create an instance of SearchActivity and send the user there, since the user had been in this task fairly recently. As with your app shortcut scenario, the user can wind up in SearchActivity without having visited the launcher activity during the life of your current process.

    Activities either need to be independent of all other activities, or they need to be aware of their dependencies and be able to cope when those dependencies are not being met (e.g., redirect the user back to the launcher activity).