I have a problem. I have 2 activities
, the main and a second screen. I want the 2nd screen to send data to the main screen but when I start the application it tells me that it has stopped and closes. When I do it the other way around (From the main page to the 2nd screen) it works without problems.
Does anyone know why the error? To pass the data I am using Bundle
, is it the correct way or is there a better one?
Main screen :
public class MainActivity extends AppCompatActivity {
TextView tv2;
Button b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv2=(TextView)findViewById(R.id.tv2);
b2=(Button)findViewById(R.id.b2);
Bundle parametros = this.getIntent().getExtras();
String datos = parametros.getString("datos");
tv2.setText(datos);
}
public void segunda_pantalla(View view){
Intent i=new Intent(this, segunda_pantalla.class);
startActivity(i);
}
Second Screen :
public class pantalla2 extends AppCompatActivity {
Button b1;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pantalla2);
b1=(Button)findViewById(R.id.b1);
tv1=(TextView)findViewById(R.id.tv1);
}
public void b1(View view){
tv1.setText("1");
String datos = tv1.getText().toString();
Bundle parmetros = new Bundle();
parmetros.putString("datos", datos);
Intent i = new Intent(this, MainActivity.class);
i.putExtras(parmetros);
startActivity(i);
}
How to send data between Activities.
To send data, it is generally done through a
Bundle
in which values can be added and thatbundle
is sent through aIntent
. You can specify the delivery of any type of element or array of elements by specifying the name:The values are obtained in the
Activity
one that receives theBundle
by means of getExtras() or the specific method to obtain received data type . Returns null if no value is found.Or simply:
###* Important: it is important to mention that the reception of the information in the
Activity
destination must always be done within the methodonCreate()
.Your problem occurs because when you start it
MainActivity
tries to receive a bundle which has a valuenull
, you can verify it in the LogCat.Perform the following validation:
Take into account that in the activity that you do not ensure receives a
Bundle
, you have to perform this validation.It is also important to comment that to return to the first
Activity
one you do not need to do it through aIntent
, simply usefinish()
to close it.To send data between activities, the Bundle class is used.
The method can be used
putExtra()
to attach integers, floating point values, bytes, characters, and some other data types that are listed in the official Android developer documentation. To retrieve these values fromIntent
theActivity
receiver, Service orBroadcast Receiver
the method is usedgetExtra()
.Likewise, we can store any number of key-value pairs in a Bundle object and simply pass this object through the
Intent
:Other types that can be shipped:
Other similar questions on the site that would be of great help to you:
Pass Data from Fragments to Fragments in another activity
Help with android studio various activities, getting data
That is not the correct way to receive the data, there is a method called
startActivityForResult()
:and to return data from the second activity:
And the application stops because when you run the app the first time it is asking for data in
onCreate()
it but there is no such data:Here is some of the official documentation of StartActivity() and StartActivityForResult()
Note The function
finish()
should be used almost compulsorily, otherwise when you enter the second activity and it returns you to the first you will be cycling between activities, this can be tested when you press the back button it will return you to the second activity, then to the first and at the end it will exit the application.Curious fact: If you review the Activity Lifecycle you will notice that when a second activity is executed the current activity is put into
onPause()
therefore when you return to the main activity the method is executedonResume()
instead ofonCreate()
. (Just some tips to optimize your code)In the first activity you have to check that your variable
paremetros
is not nulltry something like this
I would do it as follows Activity 2
And in MainActivity I receive it
Ready you have them
to receive from Window2
window2 that sends to vebtana1
And you get it like this: