1

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);
            }
        }
        ...
    }
    ...
}
m00am
  • 5,910
  • 11
  • 53
  • 69
reynard80
  • 105
  • 2
  • 6
  • Store your data in [SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences.html) and load it when you start `MainActivity.` Bundle is used to pass data between activities. – sharp Jan 09 '18 at 12:21

1 Answers1

0
  1. If you want persistent storage, you can store your data in Bundle using SharedPreferences but using a helper method as shown in the accepted answer of this question. There are other ways to store your data as well, either in a file or database.

  2. Refer to this. It has been nicely explained by @Magius. In short, if you are launching the activity from the launcher, getIntent().getAction() will return android.intent.action.MAIN and getCategories() will return a set which will contain the android.intent.category.LAUNCHER category. You can check for this in your onCreate() to know where you are coming from. In case where you are launching the activity from launcher, sourceExtras will be null and you can instead set it to the Bundle you have saved.

Pulak
  • 768
  • 7
  • 16
  • that's funny because the posts you link to say that you cannot store a bundle in sharedpreferences – Tim Jan 09 '18 at 12:54
  • Technically you cannot store a bundle directly in `SharedPreferences` but you can do so with the help of helper method that iterates through the bundle and saves each data. Maybe you didn't look at the accepted answer @TimCastelijns – Pulak Jan 09 '18 at 12:57
  • @TimCastelijns I've made edits to avoid confusion.Feel free to edit it further if you feel I've not framed it right. Thanks. – Pulak Jan 09 '18 at 13:03
  • Wait, you can iterate over the elements of Bundle? How? – EpicPandaForce Jan 09 '18 at 13:21
  • I've been working on a solution, using SharedPreferences. However, it isn't possible to store arrays in SharedPreferences, right? So should I then convert my Array to seperate long values for storing in the SharePreferences? And put them back together in an Array when reading them? – reynard80 Jan 16 '18 at 11:09