ALLOMI Raj ALLOMI Raj - 1 month ago 17
Android Question

realm results empty after clearing app from multitask using a service

i have a service that downloading some files using

ion
library and when the download finished i want to save the downloaded files to a database
realm database

the problem is when the user clearing the app from multitask the app crashes and shows
ArrayIndexOutOfBoundsException
in logcat
i think the problem is that the model class getting destroyed when app is cleared

here is the code

//Service Code

List<Surah> surahList;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null)
reciterName = intent.getStringExtra("reciterName");

surahList = realm.where(Surah.class).equalTo("reciterName", reciterName).findAll();
Surah surah = surahList.get(10);
String folderName = surah.getReciterName() + " " + surah.getRewaya();
File path = new File(Environment.getExternalStorageDirectory() + "/Quran Sound/" + folderName + "/" + surah.getSurahNumberMp3());


NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder
.setSmallIcon(R.drawable.ic_stat_noti);
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(2, builder.build());
Ion.with(this)
.load(surah.getSurahLink())
.write(path)
.setCallback(new FutureCallback<File>() {
@Override
public void onCompleted(Exception e, File result) {
Toast.makeText(DownloadService.this, result + "", Toast.LENGTH_SHORT).show();
Log.d("3llomi", "ion Download Finished!");
notificationManager.cancel(2);
stopSelf();
}
});

return START_STICKY;
}


//Logcat

FATAL EXCEPTION: main
Process: com.devlomi.quransound, PID: 4424
java.lang.RuntimeException: Unable to start service com.devlomi.quransound.DownloadService@8f466f2 with null: java.lang.ArrayIndexOutOfBoundsException: rowIndex > available rows: 10 > 0
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3027)
at android.app.ActivityThread.-wrap17(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ArrayIndexOutOfBoundsException: rowIndex > available rows: 10 > 0
at io.realm.internal.TableView.nativeGetSourceRowIndex(Native Method)
at io.realm.internal.TableView.getSourceRowIndex(TableView.java:128)
at io.realm.RealmResults.get(RealmResults.java:205)
at io.realm.RealmResults.get(RealmResults.java:70)
at com.devlomi.quransound.DownloadService.onStartCommand(DownloadService.java:104)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3010)
at android.app.ActivityThread.-wrap17(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1442) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Answer

ok the problem was that the reciterName that comes from an Activity to Service using intent getting destroyed when clearing the app from multitask so i change from return START_STICKY to return START_REDELIVER_INTENT