How to Manage Startactivityforresult on Android

How to manage startActivityForResult on Android

From your FirstActivity, call the SecondActivity using the startActivityForResult() method.

For example:

int LAUNCH_SECOND_ACTIVITY = 1
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, LAUNCH_SECOND_ACTIVITY);

In your SecondActivity, set the data which you want to return back to FirstActivity. If you don't want to return back, don't set any.

For example: In SecondActivity if you want to send back data:

Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();

If you don't want to return data:

Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();

Now in your FirstActivity class, write the following code for the onActivityResult() method.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == LAUNCH_SECOND_ACTIVITY) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == Activity.RESULT_CANCELED) {
// Write your code if there's no result
}
}
} //onActivityResult

To implement passing data between two activities in a much better way in Kotlin, please go through 'A better way to pass data between Activities'.

How to use StartActivityForResult()

You can request user input in a number of ways, but if you want to use a new Activity, as you mentioned, we can use startActivityForResult() to launch a new activity and return the input from there.

Firstly, I would highly recommend reading through this Stack Overflow answer about how to use startActivityForResult(). I will explain how you can implement it for your specific use case.

So, you need to understand that startActivityForResult() has two parameters:

  • the Intent (which you use to pass data between activities)
  • a "request code" integer that identifies your request

It is good practice to use a constant for your request code that you can access in both of your activities. For example, in your main activity, you could add:

public static final int REQUEST_CODE = 1;

This would be accessible in both activities since it is public, and it would work for a request code since it is an int (integer).

In your main activity, you need an action (such as a button press) to start your second activity. Let's assume it is a button click that triggers this action:

Button button = (Button) findViewById(R.id.your_button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// actions that will happen when the button is pressed:

Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
});

Basically, we are creating an Intent which specifies our current activity (this) and the second activity. We use the intent in the startActivityForResult() method along with the REQUEST_CODE we declared earlier.

Now, in our second activity, we need something to trigger the data being returned back. From the previous question you asked, I'm assuming you want data to be returned to the main activity when a RecyclerView item has been clicked. Here is part of my answer to that question modified to show how data will be sent back.

ExampleClickAdapter clickAdapter = new ExampleClickAdapter(yourObjects);
clickAdapter.setOnEntryClickListener(new ExampleClickAdapter.OnEntryClickListener() {
@Override
public void onEntryClick(View view, int position) {
Intent intent = new Intent();
intent.putExtra("pos", position);
setResult(Activity.RESULT_OK, intent);
finish();
}
});
recyclerView.setAdapter(clickAdapter);

The above will send back the position of the list item from the RecyclerView clicked.

Have a look at the Intent's putExtra() method. This is what passes data. You can see that I have passed a String "pos" and the variable for the item position in this method, but why:

intent.putExtra("key", object);

The putExtra method of Intents always use a String key and another object, such as your integer variable, position. The key will be used again when retrieving this object (as I will show you later).

We use Activity.RESULT_OK to say that we are passing a result, but you can use Activity.RESULT_CANCELED if you don't want to send back a result - this is explained in the answer link I mentioned at the beginning. Note that if you use Activity.RESULT_CANCELED, you would not need to use putExtra as there would be nothing to send back.

Finally, you need to add something into your main activity which will deal with receiving the results from your second activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == REQUEST_CODE) {

if (resultCode == Activity.RESULT_OK) {
int result = data.getIntExtra("pos");
// do something with the result

} else if (resultCode == Activity.RESULT_CANCELED) {
// some stuff that will happen if there's no result
}
}
}

We use the onActivityResult method for this.

At the beginning of this method, we are checking to see if the requestCode is the same as the one we defined earlier (as a public constant, REQUEST_CODE).

If so, we continue and check to see what the resultCode of the result was. If data was sent back (Activity.RESULT_OK), we can retrieve it using getIntExtra() as the position was an integer (similarly, use getStringExtra() if you are returning a String). Then you can do something with the returned data. However, if data was not sent back (as we mentioned earlier with Activity.RESULT_CANCELED), you can do something else.

Hopefully this helps you with implementing your idea, but a Google search would've found the answer I mentioned above (here is the link again) which clearly explained how to use startActivityForResult(). Other answers for this question also explain it well, but perhaps you needed guidance on how to implement it in your use case (i.e. combined with your code from the previous question you had), which is why I've provided the explanation above.

You can also read the Android documentation on Getting a Result from an Activity as well as Android documentation for the [startActivityForResult() method](https://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)) and Intents.

startActivityForResult on Android

in your MainActivity change:

Intent intent = new Intent(getApplicationContext(),DeviceListActivity.class);
int resultCode = -1;
startActivityForResult(intent,resultCode);

to

Intent intent = new Intent(getApplicationContext(),DeviceListActivity.class);
int resultCode = 1;
startActivityForResult(intent,resultCode);

and in your DeviceListActivity replace your code with:

Intent returnIntent = new Intent();
String result = "Tout va bien!";
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK, returnIntent);
finish();

Hope it will help you out.

How to maintain startActivityForResult for single activity

Remove super.onBackPressed(); in C's onBackPressed method,please have a try.



Related Topics



Leave a reply



Submit