Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage startActivityForResult on Android

In my activity, I'm calling a second activity from the main activity by startActivityForResult. In my second activity, there are some methods that finish this activity (maybe without a result), however, just one of them returns a result.

For example, from the main activity, I call a second one. In this activity, I'm checking some features of a handset, such as does it have a camera. If it doesn't have then I'll close this activity. Also, during the preparation of MediaRecorder or MediaPlayer if a problem happens then I'll close this activity.

If its device has a camera and recording is done completely, then after recording a video if a user clicks on the done button then I'll send the result (address of the recorded video) back to the main activity.

How do I check the result from the main activity?

like image 262
Hesam Avatar asked May 02 '12 03:05

Hesam


People also ask

How do you manage startActivityForResult?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is startActivityForResult in Android?

By the help of android startActivityForResult() method, we can send information from one activity to another and vice-versa. The android startActivityForResult method, requires a result from the second activity (activity to be invoked).

What is alternative for startActivityForResult in Android?

In this article, we will discuss the Activity Result API, an alternative to the deprecated startActivityForResult + onActivityResult methods. We will use Kotlin in this article for the code samples. Android introduced the Activity Result APIs as a major change in the androidx.

Can startActivityForResult still be used?

onActivityResult , startActivityForResult , requestPermissions , and onRequestPermissionsResult are deprecated on androidx.


1 Answers

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'.

like image 164
Nishant Avatar answered Sep 19 '22 22:09

Nishant