-3

From now I just put: finish(); but it only makes me return to the parent activity and I want to pass values. Thanks in advance.

Don Dev
  • 61
  • 8

3 Answers3

1

If you want to open another activity sending some string or anything use:

                    Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
                    intent.putExtra("ReferencedWord","String With Whatever You Want");
                    int CodigoPeticion;
                    CodigoPeticion=2;
                    startActivityForResult (intent,CodigoPeticion);
                    finish();

referencedWord is the key to open the string in the other activity.

In Another Activity use this in your onCreate

    String word="";
    Bundle extras = getIntent().getExtras();
    if (extras!= null) {
                word = extras.getString("ReferencedWord");
                Toast.makeText(getBaseContext(),"String given by MainActivity: "+word,Toast.LENGTH_LONG).show();
    }
Carlos Carrizales
  • 2,340
  • 3
  • 18
  • 24
  • Hi, your code works but I lose the data of the parent activity, I want to preserve it – Don Dev May 06 '16 at 18:03
  • hi, ok just change the scope of the variable word, inicialize it before use the bundle extras, after it, you got the data from the last activity, use it as you want – Carlos Carrizales May 07 '16 at 01:51
0

when you go form first activity to second activity. the first activity become pause. so when you finish the second activity the first activity is again resume

you want to pass some value from second activity to first activity

so just finish the first activity and start second activity when you are coming back to first activity just intent from second to first activity and attached the bundle message to it after it finish the second activity

Nouman Shah
  • 534
  • 1
  • 9
  • 22
  • Paragraphs one and two are clear, can't get the last, can you explain more detailed? Thanks in advance. – Don Dev May 06 '16 at 18:09
0

If you wanna pass any values from one activity to another activity

Intent sendVal = new Intent(this,NextActivity.class);
sendVal.putExtra("key","any values");   //key is your private key for sending values
startActivityForResult(sendVal,4);   //request code
finish();

In next Activity,you can retrieve those values from PreviousActivity :

Bundle extraVal = getIntent().getExtras();

   //getting values from extraVal
 String myString = extraVal.getString("key");  //so our value is "any values"

 Toast.makeText(getApplicationContext(),"Your string from previous Activity"+myString,Toast.LENGTH_LONG).show();

By this way,you can retrieve values from PreviousActivity.

If you wanna get values from NextActivity to PreviousActivity then override the onActivityResult method and retrieve your values from intent parameter(3rd) of onActivityResult method by matching your request code.

onActivityResult method invokes when you come back from NextActivity to PreviousActivity

Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70