As a novice Android/Java developer, I hope this is not a trivial question. I've been struggling with it for hours now though, so I hope to get some pointers.
I am actually developing an alarm/timer application with multiple intervals for a specific timing purpose. My idea is to create a Bundle with the timing parameters (amount of intervals, interval duration, and a schema with the actual calculated alarm times). This Bundle is created in the MainActivity UI. While an alarm schema is running I want to store these parameters in a Bundle that I pass to a notification that is always visible while an alarm schema is active.
The user can get back to the MainActivity UI from the notification; in this case, the notification passes the Bundle to the MainActivity. However, when the user returns to the app again via the launcher icon, the Bundle is not available, as there is no Intent with the needed data.
1) Now my first question may be a broader question, as I am wondering how I can solve this type of problem. Is it possible to 'store' the data in a Bundle, or should I store the data in a file, and read from the file everytime the MainActivity is opened?
2) More specifically to my code (see below), I don't understand why my app crashes when I (re)open the MainActivity from the launcher. In that case, I would think that there wouldn't be an Intent with a Bundle, so . Apparently, this is not the case, because the first is run and the app crashes with a NullPointer. Where do this Intent and empty Bundle come from?
public class MainActivity extends AppCompatActivity {
int intervalAmt;
int intervalDur;
long[] alarmSchema;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent sourceIntent = getIntent();
Bundle sourceExtras = sourceIntent.getExtras();
if (sourceExtras != null) {
intervalDur = sourceExtras.getInt("intervalDur", 0);
intervalAmt = sourceExtras.getInt("intervalAmt", 0);
alarmSchema = sourceExtras.getLongArray("alarmSchema");
}
if (sourceExtras == null) {
PreferenceManager.setDefaultValues(this, R.xml.settings, false);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
intervalAmt = Integer.valueOf(sharedPreferences.getString("edittext_intervalAmt", "4"));
intervalDur = (Integer.valueOf(sharedPreferences.getString("edittext_intervalDur", "1200000")));
alarmSchema = new long[intervalAmt+1];
for(int i=0; i<=(intervalAmt-currentInterval); i++){
alarmSchema[currentInterval+i] = System.currentTimeMillis() + (intervalDur*i);
}
}
...
}
...
}