Search code examples
androidextras

How to replace an extras value?


I am a beginner in android programming and I couldn't find a way to replace an extra value

 @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    list_1= new Intent(this,Ekran1.class);
    list_2= new Intent(this,Ekran2.class);
    list_2.putExtra("iname", "2");
    if(position==0)
    {
        startActivity(list_1);
        if(list_2.getExtras().getString("iname")=="2")
        {
            Toast.makeText(getApplicationContext(), "iname=2", Toast.LENGTH_SHORT).show();
            stopService(list_2);
        }
        else if(list_2.getExtras().getString("iname") =="3")
        {
            Toast.makeText(getApplicationContext(), "iname=3", Toast.LENGTH_SHORT).show();
            stopService(list_2);
        }
    }
    else if(position==1)
    {
        list_2.putExtra("iname", "1");
        startActivity(list_2);
        stopService(list_1);
    }

I want to change iname value to 1 but it has always value 2.


Solution

  • First of all, compare Strings with equals()

    if (list_2.getExtras().getString("iname").equals("2"))
    

    The values in extras are not intended to be replaced. You put extras to pass the data when Activity is started.

    It's hard to understand your intentions, but if the iname must be changed in Ekran, you should rather call

    startActivityForResult(ekranIntent, requestCode)
    

    and analyze the data returned in

    onActivityResult()
    

    Overridden in your current Activity.