0

How can I pass two values to another fragment. I am passing one parameter (log). Now I want to add another parameter "id".

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity().getApplicationContext(), ViewMyCommande.class);
        HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
        String tid = map.get("log").toString();
        intent.putExtra("log", tid);
        startActivity(intent);
    }

I am retrieving its value like this:

String log = getArguments().getString("log");
Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
sonia_
  • 21
  • 4

4 Answers4

2

U can send one more parameter in a similar way you are sending/retrieving the first parameter

intent.putExtra("keyName", "somevalue");
getStringExtra(String keyName)

or you can compose an object with all the values and send along with the intent.

i.putExtra((Parcelable) myParcelableObject);
or
i.putExtra((Serializable) myParcelableObject);
deepThought
  • 699
  • 4
  • 26
0

are we talking about fragments or activities??

if you wanna pass data to other activities you must use intent like you and use

intent.putExtra("key", data);

for each data that you will pass

and if you wanna pass data to fragment you can use new instance in you fragment class

public static MyFragment newInstance() {

    Bundle args = new Bundle();

    MyFragment fragment = new MyFragment();
    args.putParcelable("key",data);
    fragment.setArguments(args);
    return fragment;
}

you must declare your object like as parcelable

i hope that this will help you

Nygar
  • 1
  • 1
0

create a variable in fragment main activity like

String message;

suppose message value is "frag"; and use it from any fragment of this activity like

if(fragment_main_activity_name.message.equals("frag"))
    {

    }

enjoy happy coding :) please make it as right if its helpful :)

snehasish
  • 171
  • 2
  • 10
0

You could do something like this.

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(getActivity().getApplicationContext(), ViewMyCommande.class);
    HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
    String key = "log";
    intent.putExtra("key", key);
    intent.putExtra("value", map.get(key))
    startActivity(intent);
}

PS: In your code you have a line like this String tid = map.get("log").toString(); as your map is already a <String, String> map, there is no need to call Object::toString in it. Just call String tid = map.get("log") that will get you a String.

Another suggestion is to set this key "log" that you are using as a constant, than accessing it from the bought Activities. You would add a field like this into your Activity class private static final String MAP_KEY = "log"; than you could use it in you code, so in the code above, you could remove the String key = "log"; line and replace key by your constant, so the two intent lines would look like this:

intent.putExtra("key", MAP_KEY);
intent.putExtra("value", map.get(MAP_KEY))

Another suggestion is to extract the bouth keys that you're using in putExtras as public constants, then use these in the Activity that you are receiving the data. Something like this:

public static String KEY = "key";
public static String VALUE = "value";

Then use it in the intent lines.

intent.putExtra(KEY, MAP_KEY);
intent.putExtra(VALUE, map.get(MAP_KEY))

When in you SecondActivity you can get these values as getArguments().getString(FirstActivity.KEY); same for value.

Another observation, if your key will be constant you don't really need to send the key to your SecondActivity.

If you need all these map in your SecondActivity you could call putExtra and place you map in it. Then in SecondActivity you would need yo call HashMap<String, String> firstActivityMap = (HashMap<String, String>) getSerializable(MAP)

OBS: maybe it's a bit confusing, I hope community could help to improve this answer

wviana
  • 1,619
  • 2
  • 19
  • 47