1

Assume I have a "post submission" feature on my application. There are two activities and layouts:

  1. Main post activity
  2. Location selection activity

How can I pass variables between them when they're already opened? I already know how when they haven't been opened. I use "singleTask" mode to prevent multiple instances.

Reason I need this is because there's a button that links to LocationSelectionActivity on the MainPostActivity. I want when someone clicked the button, select the location, then go back to the MainPostActivity without any activity restart so the filled fields will not reset.

Gak2
  • 2,661
  • 1
  • 16
  • 28
Sena
  • 178
  • 1
  • 13

3 Answers3

0

Use onActivityResult() with startActivityForResult().

Reference: https://developer.android.com/training/basics/intents/result

Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126
0

You're probably looking for onNewIntent, you can override it in your Activity class and retrieve the new Intent in your onResume method.

    /**
     * Override super.onNewIntent() so that calls to getIntent() will return the
     * latest intent that was used to start this Activity rather than the first
     * intent.
     */
    @Override
    public void onNewIntent(Intent intent){
        super.onNewIntent(intent); // Propagate.
        setIntent(intent); // Passing the new intent to setIntent() means this new intent will be the one returned whenever getIntent() is called.
    }
LS_
  • 6,763
  • 9
  • 52
  • 88
0

You should not use Activities for this, you are in your own "main process" so you should be using 1 Activity and 2 Fragments for this.

Then you can use locationSelectionFragment.setTargetFragment(this); in MainPostFragment before it is added to Activity's fragment transaction, this lets you use getTargetFragment() to just pass back the value:

((MainPostFragment)getTargetFragment()).giveValueBack(value);

But there are multiple other options each with their own strengths and weaknesses as described in this article.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428