Your experience on this site will be improved by allowing cookies
The result from another activity can be received by using the android startActivityForResult() method, i.e., the information from one activity can also be sent to another activity and is the same for vice-versa, by using the android startActivityForResult() method. A result is needed by the android startActivityForResult method from the second activity, i.e, the activity to be invoked. The onActivityResult method thus needs to be overridden when the second activity returns the result, i.e, it needs to be invoked automatically when the second activity returns the result.
The two variants of startActivityForResult() method are:
In the below example, we are demonstrating the usage of the android startActivityForResult method.
activity_main.xml:
In the activity_main.xml file, we will drag a textview and a button from the palette.
Activity class:(File: MainActivity.java)
In the MainActivity.java file, we will write the code to invoke another activity and to retrieve the result from that activity.
package com.example.radioapp; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView actionEvent; @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); actionEvent = findViewById(R.id.actionEvent); actionEvent.setText("Click Here"); actionEvent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, SecondActivity.class); startActivityForResult(i, 1); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1) { if (resultCode == RESULT_OK) { String returnString = data.getStringExtra("result"); actionEvent.setText(returnString); } } } } |
SecondActivity class:
To create a new activity follow the below steps:
In the SecondActivity.java file, we will write the code to display the content of the second activity layout file.
File: SecondActivity.java:
package com.example.radioapp; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Intent returnIntent = new Intent(); returnIntent.putExtra("result","Hello World!!"); setResult(Activity.RESULT_OK,returnIntent); finish(); } } |
Activity_second.xml:
On creating the above activity, a new XML file will be automatically created. In the second_main.xml file, we will drag an editText, a textView and a button from the palette.
Android Manifest:(File: AndroidManifest.xml)
0 comments