user14396608
user14396608

Reputation:

How do you pass data back and forth between activities in Android?

I have two activities that should pass data back and forth to each other using intents. I'm not sure where to place some of the puts and gets though.

For Activity1 (MainActivity), I have a button, and on press it creates an intent, and then puts it to Activity2, then starts it using startActivity(intent).

btn.setOnClickListener((v) -> {
    Intent intent = new Intent(this, Activity2.class);
    intent.putExtra("test", testClass);
    startActivity(intent);
});

Then in Activity2, I get that information in the onCreate() function using getIntent.

Now what I want to do is have a button in Activity2 that will pass data to Activity1, but won't necessarily "start/show" the activity.

So I'm wondering how this can be done.

My idea is to have the following similar to before:

Activity2:

btn.setOnClickListener((v) -> {
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("info", info);
});

But I'm confused about two things

  1. Can I do this without starting the activity right away
  2. Where would I do the getIntent call in MainActivity to retrieve this data

Upvotes: 1

Views: 3842

Answers (4)

MitchHS
MitchHS

Reputation: 353

There are several ways you can achieve this pending on what type of data you're looking at passing.

If it's an entire class object then make the class parcable. You can copy and paste the class in this website http://www.parcelabler.com/ and it auto formats it for you. All you do is pass it as an intent extra.

If you're looking at data that needs an action performed on it and then passed to another activity I would suggest using and intent service. You can perform certain actions pending the intents received in the service.

If you're looking at performing certain actions only after XYZ has occurred in your application then used shared preferences. These can be accessed anywhere quite easily.

Lastly, if you're using bulk data consistently that needs to remain persistent, use a local database storage and just query when you need to.

Upvotes: 0

Slava
Slava

Reputation: 1

You can use SharedPreferences or static variables/fields for that.

Upvotes: -1

Hayssam Soussi
Hayssam Soussi

Reputation: 1083

You can use startActivityForResult to start Activity2 and receive a result back to Activity1

Activity 1

int LAUNCH_ACTIVITY_TWO = 1;
Intent i = new Intent(this, Activity2.class);
i.putExtra("test", testClass);
startActivityForResult(i, LAUNCH_ACTIVITY_TWO);

//onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == LAUNCH_ACTIVITY_TWO) {
        if(resultCode == Activity.RESULT_OK){
            String result=data.getStringExtra("result");
        }
    }
}

Activity 2

Intent returnIntent = new Intent();
returnIntent.putExtra("result", result);
setResult(Activity.RESULT_OK, returnIntent);
finish();

Full Activity 1 code:

 public class MainActivity extends AppCompatActivity {
    public static int LAUNCH_ACTIVITY_TWO = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.btn);

        btn.setOnClickListener((v) -> {
            Intent i = new Intent(this, Activity2.class);
            i.putExtra("test", testClass);
            startActivityForResult(i, LAUNCH_ACTIVITY_TWO);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == LAUNCH_ACTIVITY_TWO) {
            if(resultCode == Activity.RESULT_OK){
                String result= data.getStringExtra("result");
            }
        }
    }
}

Full Activity 2 code:

public class Activity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(getIntent().getExtras() != null) {
            // Get intent extras from Activity 1
        }
        
        Button btn = (Button) findViewById(R.id.btn);

        btn.setOnClickListener((v) -> {
            Intent returnIntent = new Intent();
            returnIntent.putExtra("result", result);
            setResult(Activity.RESULT_OK, returnIntent);
            finish();
        });
    }
}

Upvotes: 3

Dominik Wuttke
Dominik Wuttke

Reputation: 555

You can for example create a list in activity2 to which you add the data you want to pass to activity1. Before starting activity1 you get the values out of your array and add them as extras to the input. You can have the data added to a JSONObject and use a serializer so you don't have to add your items all by yourself.

If you want to pass your data to activity1 without starting it,so activity1 processes the data, that is not possible. Activity doesnt execute anything while you are in activity2, for such cases you use fragments.

If you use fragments you can put all data in a viewmodel which is bound to the activity instead of each fragment.

Upvotes: 0

Related Questions