0

I am new to Android and I'm stuck because of this problem for hours.

I have 3 activities in my project, let's say they're A, B, and C. A is the MainActivity.

A has the data B needs so B should be started after A sends the data to it with intent. I have a bottom navigation menu for each activity. When user tries to start B from Activity C,

1) I want B to not be started if A wasn't started before C
2) If A WAS started before C, I want B to be started with the received data from A.

Currently I just set intent to the button listener in C to go to B but when B has no data from A, B crashes.

I don't know if these two would be possible? I've tried finish(), onBackPressed(), etc. but none of those worked as I wanted. Any help would be appreciated. Thanks.

Yoojin Kim
  • 37
  • 4
  • See [How to trigger a function on a old Activity when returning from a newer activity?](https://stackoverflow.com/a/54058428/8034839) – shizhen Mar 28 '19 at 08:38
  • Is it really required that you have 3 Activities as some kind of learning exercise? You mention having a bottom nav menu, which I assume you're using to switch between these 3 activities. Is there any reason you're not implementing this a single Activity (which contains the bottom nav menu) with the 3 screens implemented as 3 Fragments? Then if you need to share data you can have a common ViewModel between them. – Ryujin Mar 28 '19 at 08:53
  • @shizhen tried that but not exactly what I wanted – Yoojin Kim Mar 28 '19 at 08:56
  • @Eugene I am so new to Android I didn't know how to use Fragments when I first started this project. And there are more activities in each ones so I dont know if I'm able to switch those to fragments now – Yoojin Kim Mar 28 '19 at 08:58

1 Answers1

0

Instead of passing data to activity how about storing that data into sharedpreferences which could easily solve your problem. At runtime you can always check whether sharepreference is null or not and if it's not null you can perform your desired action with stored data.