-1

I would like to pass data between activities. When I use one activity (Details), everything works fine, but when I add a second activity (MapsActivity), the application ignores (Details) and transfers data only to MapsActivity. How can I fix it? Thanks in advance

 holder.itemView.setOnClickListener(v -> {
        Intent mIntent = new Intent(context, Details.class);
        Intent mapIntent= new Intent(context,MapsActivity.class);
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mapIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


        mIntent.putExtra("Country_CurrencyCode", pozycja.getCurrencies().get(0).getCode());
        mIntent.putExtra("Country_CurrencyName", pozycja.getCurrencies().get(0).getName());
        mIntent.putExtra("Country_CurrencySymbol", pozycja.getCurrencies().get(0).getSymbol());

        context.startActivity(mIntent);

        mapIntent.putExtra("Country_Lat",pozycja.getLatlng().get(0));
        mapIntent.putExtra("Country_Lng",pozycja.getLatlng().get(1));

        context.startActivity(mapIntent);
karthik
  • 528
  • 4
  • 19
  • What do you want to achieve. – hardartcore Aug 06 '18 at 15:50
  • I would like to pass data to Details.class and MapsActivity.class – smitzer vojtech Aug 06 '18 at 15:51
  • 2
    And why you are starting two activities at the same time? – hardartcore Aug 06 '18 at 15:52
  • Why do you use `Intent.FLAG_ACTIVITY_NEW_TASK`? –  Aug 06 '18 at 15:52
  • Given that you can only show one activity at a time, this seems like you are just trying to make a workaround to make some data persistent across multiple Activities. I'd suggest reading different options [here](https://stackoverflow.com/questions/4878159/whats-the-best-way-to-share-data-between-activities) and looking into different ways of sharing/storing data between multiple activities. Seems like using SharedPreferences would work a lot better in this case. – Tyler V Aug 06 '18 at 16:37

1 Answers1

0

This is a behavior I did not expect but after testing your code, I found these:
You start 1st the Details activity and 2nd the MapsActivity.
The result is that you see on the screen the 2nd activity: MapsActivity.
For this activity you can check that it's got all the extra values that you passed.
What is happening is that although you have also started the 1st activity: Details, its onCreate() has not yet been invoked, but it will be invoked as soon as you close the 2nd activity MapsActivity and then you will see that its extras are there as you put them.
So the 1st activity gets the extras fine but until its onCreate() is called you can't access them.
So a workaround:
Start only Details activity but pass to it not only its own extras but MapsActivity's extras also.
In Details's onCreate() start MapsActivity with its own extras.

  • @smitzervojtech you know with your problem I learned something too, so thank you! –  Aug 06 '18 at 16:59