0

I am making an application where my activities navigation is set up like this

Main Activity <-> Results -> End

My application will be sending data through intents from the Main Activity to the Results Activity where the Results Activity will calculate and display the data. The Results Activity will either return the calculated results back to the Main Activity or determine that the conditions have been met and to start the End Activity. The End Activity will be able to navigate back to Main Activity to start the application over passing no data over.

My problem is I can't figure out how to effectively send the data back to the Main Activity from the Results Activity while having the option to send the data to the End Activity once the conditions have been met. While doing research I found the method startActivityForResult however my dilemma is that my Results Activity may not always return a result back to the Main Activity once the conditions have been met.

Should I use startActivityForResult for the Main Activity and Result Acitivity and start a new activity for End Activity once the condition has been met or would using Shared Preferences be a better option in this situation?

  • Why are you using three activities instead of one? – CommonsWare Dec 14 '17 at 21:56
  • @CommonsWare Can you please explain this or provide links? I've tried looking into it but I haven't found enough resources to implement it on my own or fully understand it. –  Dec 14 '17 at 22:44
  • I am not certain what you are looking for me to provide links to. You decided to implement three activities. I do not know why, which is why I asked the question that I did. You could be doing all of this in one activity, updating the UI as you proceed (e.g., via fragments, via [a wizard-style UI library](https://github.com/Nimrodda/WizarDroid)). – CommonsWare Dec 14 '17 at 22:58
  • @CommonsWare Sorry what I meant was a link as an example of using one Activity and updating the UI within it. I have looked into fragments a lot but from my understanding it is to be used when the activities have similar components. I saw this post previously https://stackoverflow.com/questions/20306091/dilemma-when-to-use-fragments-vs-activities. Most of the activities are similar except for the Main Activity which is why I wasn't sure if I should use one activity with fragments while creating a separate activity for the Main Activity or create new activities for each page. –  Dec 15 '17 at 00:34
  • I am not certain where the "similar components" limitation came from. There are a few ways to implement your desired flow. The simplest, probably, is one activity and fragments for the UI steps. Or, if you really want multiple activities, look into having a singleton repository that all activities work with. – CommonsWare Dec 15 '17 at 01:47

1 Answers1

1

Check out this link: https://developer.android.com/training/basics/intents/result.html

Instead of calling startActivity(intent), you have to call: startActivityForResult(intent, requestCode (say, =2)) from main activity.

Then in your ResultActivity, you have to all your extras and info within your intent object. Before calling finish(), you have to call setData(requestCode = 2, intent).

Then, in your MainActivity, you have to override the onActivityResult() function and handle the response like:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == 2) {
        // Make sure the request was successful

    }
}
Robillo
  • 192
  • 11