Skip to content Skip to sidebar Skip to footer

Can I Pass An Intent Extra On Finish()?

I'm wondering, is it possible to send information to the activity that I return to after calling finish()? For example, I have an Activity SendMessageActivity.class which allows t

Solution 1:

Use onActivityResult.

This might help you to understand onActivityResult.

By using startActivityForResult(Intent intent, int requestCode) you can start another Activity and then receive a result from that Activity in the onActivityResult() method.So onActivityResult() is from where you start the another Activity.

onActivityResult(int requestCode, int resultCode, Intent data) check the params here. request code is there to filter from where you got the result. so you can identify different data using their requestCodes!

Example

publicclassMainActivityextendsActivity {

        // Use a unique request code for each use case privatestaticfinalintREQUEST_CODE_EXAMPLE=0x9988; 

        @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // Create an Intent to start AnotherActivityfinalIntentintent=newIntent(this, AnotherActivity.class);

            // Start AnotherActivity with the request code
            startActivityForResult(intent, REQUEST_CODE_EXAMPLE);
        }

        //-------- When a result is returned from another Activity onActivityResult is called.--------- //@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            // First we need to check if the requestCode matches the one we used.if(requestCode == REQUEST_CODE_EXAMPLE) {

                // The resultCode is set by the AnotherActivity// By convention RESULT_OK means that what ever// AnotherActivity did was successfulif(resultCode == Activity.RESULT_OK) {
                    // Get the result from the returned IntentfinalStringresult= data.getStringExtra(AnotherActivity.EXTRA_DATA);

                    // Use the data - in this case, display it in a Toast.
                    Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show();
                } else {
                    // AnotherActivity was not successful. No data to retrieve.
                }
            }
        }
    }

AnotherActivity <- This the the one we use to send data to MainActivity

publicclassAnotherActivityextendsActivity {

        // Constant used to identify data sent between Activities.publicstaticfinalStringEXTRA_DATA="EXTRA_DATA";

        @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_another);

            finalViewbutton= findViewById(R.id.button);
            // When this button is clicked we want to return a result
            button.setOnClickListener(newView.OnClickListener() {
                @OverridepublicvoidonClick(View view) {
                    // Create a new Intent as container for the resultfinalIntentdata=newIntent();

                    // Add the required data to be returned to the MainActivity
                    data.putExtra(EXTRA_DATA, "Some interesting data!");

                    // Set the resultCode to Activity.RESULT_OK to // indicate a success and attach the Intent// which contains our result data
                    setResult(Activity.RESULT_OK, data); 

                    // With finish() we close the AnotherActivity to // return to MainActivity
                    finish();
                }
            });
        }

        @OverridepublicvoidonBackPressed() {
            // When the user hits the back button set the resultCode // to Activity.RESULT_CANCELED to indicate a failure
            setResult(Activity.RESULT_CANCELED);
            super.onBackPressed();
        }
    }

Note : Now check in MainActivity you startActivityForResult there you specify a REQUEST_CODE. Let's say you want to call three different Activities to get results.. so there are three startActivityForResult calls with three different REQUEST_CODE's. REQUEST_CODE is nothing but a unique key you specify in your activity to uniquely identify your startActivityForResult calls.

Once you receive data from those Activities you can check what is the REQUEST_CODE, then you know ah ha this result is from this Activity.

It's like you send mails to your lovers with a colorful covers and ask them to reply in the same covers. Then if you get a letter back from them, you know who sent that one for you. awww ;)

Solution 2:

You can set result of an activity, which allow you to data into an initent.

In your first activity, call the new one with startActivityForResult() and retrieve data in method onActivityResult. Everything is in documentation.

Solution 3:

try this:

in First Activity:

Intent first = newIntent(ActivityA,this, ActivityB.class);
startActivityForResult(first, 1);

Now in Second activity: set Result during finish()

Intentintent=newIntent();
   intent.putExtra("result",result); //pass intent extra here
   setResult(RESULT_OK,intent);     
    finish();

First activity Catch the result;

protectedvoidonActivityResult(int requestCode, int resultCode, Intent data)  
   {  
     super.onActivityResult(requestCode, resultCode, data);  
      // check if the request code is same as what is passed  here it is 1  if(requestCode==1)  
             {  
                String message=data.getStringExtra("result");   
                //get the result
             }  
 }  

Solution 4:

If you call finish() to avoid that the user go back to SendMessageActivity.class, you can set this flags to your intent:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

This will open the MainActivity and remove the SendMessageActivity from the activities stack.

Post a Comment for "Can I Pass An Intent Extra On Finish()?"