0

This is harder than it sounds, which is why I'm asking for solutions.

Basically I only want the disclaimer Toast shown once per run of the app.

The app is in two parts, all are Activities. It's shown when it starts in the first part, but you can hit a menu button taking you to the second part of the app, which has another menu button to take you back to the first.

The problem is that whatever initial settings you try to make in the first part, when it starts up, are run again when returning from the second part of the app, so it'll show again.

My last idea was that in the first part's onDestroy(), when the app exits, but is not the case in this situation, you set a boolean in settings, to reset that the disclaimer can be shown, but apparently, onDestroy() is called on the first part before it goes to the second part.

Or, if you can get it to not show the first run, but behave properly every time after that, that would be okay.

And there doesn't seem to be any method to be called when the app truly is "killed", if there was that would be the way to do it, you could reset it there. Or if there was a method that was only called when the app first started..

Thanks!

user1572522
  • 569
  • 2
  • 11
  • 26
  • You could try adding a start-up activity? Essentially a blank activity that your app launches to when first opened. It displays the toast, goes to your first actual activity and destroys itself, sort of like a loading screen on games but without any UI elements. – Aryan Aug 29 '20 at 13:39
  • Yeah, I think that is the only way to go because at this time Android doesn't seem to be able to detect those things, but I'm not crazy about adding that, but I've done splash screens before on LibGdx, so I know what you're saying.. Thanks! – user1572522 Aug 29 '20 at 13:50

3 Answers3

0

You just need a boolean flag. Say we call it disclaimerShown. In onCreate() of Activity A, we check both the Intent Bundle and the savedInstanceState Bundle for this flag.

You can add the boolean to a Bundle when launching the Intent to start Activity A from Activity B.

If the user is in Activity B and presses the Back button to return to Activity A, you can override onBackPressed() in Activity B and include your flag there as well (though you'll have to catch this flag on onActivityResult() in Activity A).

If system initiated process death occurs in Activity A, the system will call onSaveInstanceState(Bundle bundle). So you add your flag to this bundle as well.

And if system initiated process death occurs in Activity B, you have nothing to worry.

And that handles all possible cases.

Gavin Wright
  • 3,124
  • 3
  • 14
  • 35
0

An elegant solution for this problem would be the ProcessLifecycleOwner. This class provides callbacks to the lifecycle of your whole app (not individual activities) and you could use the Lifecycle.Event.ON_CREATE callback to show your toast once. Look at this stackoverflow question for a usage example of the ProcessLifecycleOwner.

Oliver
  • 503
  • 1
  • 3
  • 13
0

It turns out that I already had an Activity that started before my "Activity A", and I moved my disclaimer Toast there and it works fine. You can't beat that simplicity lol.

Thanks for your answers!

user1572522
  • 569
  • 2
  • 11
  • 26