Android learning path 1: How to transmit data between activities
Transmit dataCreate Activity
Right-click the project, select New, continue to select the Activity at the bottom of the project, and finally click Blank Activity.
At this time, Android Studio automatically adds a corresponding layout file for us and adds an ID for TextView in the layout file for future use.
Send data with Intent
Create a Button control in the main layout file, define it in the main class, and set the Click listening event for it.
Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this,Main2Activity.class); i.putExtra(Thanks,Thank you for reading my blog.); startActivity(i); } }); }
Receive data with Intent
Next, you can create an Intent in the previously created Activity for acceptance. Similar to putExtra for trial sending, getStringExtra can also be used for acceptance. Of course, different methods can be used for different data types.
private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent i = getIntent(); tv = (TextView)findViewById(R.id.tv); tv.setText(i.getStringExtra(Thanks)); }
Transfer Bundle package
You can use a Bundle package to transfer data between two activities. Compared with the previous method, this package can contain more data. You can think of it as a set, this is also in line with the "package" concept.
Create Bundle
Bundle can be nested Bundle
Use the putExtras Method for Intent objects
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this,Main2Activity.class); Bundle a = new Bundle(); a.putString(Thanks, Thank you for reading my blog.); Bundle b = new Bundle(); b.putString(Android,Google); b.putString(Windows,Microsoft); a.putBundle(bundle,b); i.putExtras(a); startActivity(i); } });}
Obtain the corresponding Bundle data based on the corresponding Key value
@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main2); Intent I = getIntent (); Bundle a2 = I. getExtras (); TV = (TextView) findViewById (R. id. TV); TV. setText (String. format (% s Android belongs to % s, Windows belongs to % s, a2.getString (Thanks), and a2.getBundle (bundle ). getString (Android), a2.getBundle (bundle ). getString (Windows )));}
Pass custom object
"Classes" are indispensable in the Android development process. How can this data be transmitted.
Remember: Make sure to execute serialization, that is, add "implements Serializable"
public class Source implements Serializable{ private int source; private String ID; public int getSource() { return source; } public void setSource(int source) { this.source = source; } public String getID() { return ID; } public void setID(String ID) { this.ID = ID; } public Source(String ID, int source) { this.source = source; this.ID = ID; }}
With the previous knowledge, you can directly guess how to use it. Then, I will not be embarrassed.
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this,Main2Activity.class); i.putExtra(source,new Source(Player,1000)); startActivity(i); } });}
@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main2); Intent I = getIntent (); Source s = (Source) I. getSerializableExtra (source); TV = (TextView) findViewById (R. id. TV); TV. setText (String. format (ID is % s, score is % d, s. getID (), s. getSource ()));}
Return data from Activity
The user needs to interact with the application, and the application needs to return the data entered by the user, so ......
Define the buttons and TextView in the main Activity. The former is used to navigate to the next Activity, and the latter is used to display the returned data.
Define Button and EditText in the next Activity. The former is used to confirm and return to the main Activity, and the latter is used for user input.
In the next Activity:
Also use Intent to transmit data
Set the returned result with setResult
Finish
Private Button button; private EditText editText; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main2); editText = (EditText) findViewById (R. id. editText); button = (Button) findViewById (R. id. button2); button. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Intent I = new Intent (); I. putExtra (info, editText. getText (). toString (); setResult (0, I); // 0 indicates successful finish ();}});}
In the main Activity:
Use startActivityForResult to navigate and request the result
private Button button; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textView); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this,Main2Activity.class); startActivityForResult(i,0); } }); }
Reload the onActivityResult method and obtain data using the data parameter.
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); textView.setText(data.getStringExtra(info)); }
Okay, that's how the Activity transmits data ......