1 overview
A jump between activities is mainly used
startActivity(Intent intent);
startActivityForResult(Intent intent,int requestCode);
Copy the code
These two functions pass data using intEnts that need to be used with the Bundle. This article shows how to use these two functions to jump between activities and pass data.
2 Jump between activities
2.1 Direct Jump
There are two activities :FirstActivity and SecondActivity. Jumping from FirstActivity to SecondActivity requires only a startActivity() :
startActivity(new Intent(this,SecondActivity.class));
Copy the code
Finish () is used to indicate the end of the Activity in SecondActivity. For example, finish() is added to the button event in SecondActivity:
2.2 the callback
In many cases, you need to jump from FirstActivity to SecondActivity, do something in SecondActivity, such as updating data, and then return FirstActivity to tell FirstActivity to do something, so you need to use S tartActivityForResult(). StartActivityForResult specifies the Intent to jump to, and requestCode(int) specifies the requestCode used to call the Activity’s onActivityResult() function. Such as FirstActivity:
startActivityForResult(new Intent(this,SecondActivity.class),11);
Copy the code
Here 11 is the request code, and then in SecondActivity, use the setResult() function:
setResult(22.new Intent().putExtra("str"."from second activity"));
Copy the code
SetResult () takes two parameters, the first representing the resultCode returned from the SecondActivity, and the second Intent representing the data to be returned to FirstActivity. Finally, override onActivityResult() in FirstActivity:
3 Data transfer between activities
3.1 Passing ordinary data
PutExtra () is an Intent that takes two parameters. The first is a String, representing a key, and the second is a value. The first parameter can be an array of basic types such as byte,char,short, and long Ng also works.
3.2 Passing a set of data
PutExtra () can be used on a type-by-type basis when there are more types of data, such as a mix of ints, strings, bytes,char, etc., but it is better to use the Bundle. [intEnts] putXXX [intEnts] putXXX [intents] putXXX
3.3 Passing Objects
What if the data you want to pass is an object? Do you just use the getter for every property and put it in? No, the Bundle provides a method for handling serialized objects:
4 source
Making code cloud