Android笔记(二十) Activity中的跳转和值传递
我们知道,一个APP是由若干个Activity组成的,那么各个Acitivity中肯定需要进行跳转以及传递数值以保证App的运行,现总结一下多个Activity之间的跳转和值传递。
显式Intent跳转
AndroidManiFest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.lixyz.activitymanager"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"> </activity>
</application> </manifest>
MainActivity.java
package cn.lixyz.activitymanager; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"> <EditText
android:id="@+id/ed_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您想要传递的值"/> <Button
android:id="@+id/bt_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/> </LinearLayout>
SecondActivity.java
package cn.lixyz.activitymanager; import android.app.Activity;
import android.os.Bundle; /**
* Created by LGB on 2015/8/27.
*/
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/tv_text_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="默认文字"
android:textColor="#00ff00"
android:textSize="90sp" />
</LinearLayout>
从代码中可以看出,我们在一个Activity中,使用startActivity()传入一个Intent对象来实现Activity之间的跳转。
运行结果:


隐式Intent跳转
AndroidManiFest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.lixyz.activitymanager"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="maintosecond" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package cn.lixyz.activitymanager; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("maintosecond");
startActivity(intent);
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"> <EditText
android:id="@+id/ed_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您想要传递的值"/> <Button
android:id="@+id/bt_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/> </LinearLayout>
SecondActivity.java
package cn.lixyz.activitymanager; import android.app.Activity;
import android.os.Bundle; /**
* Created by LGB on 2015/8/27.
*/
public class SecondActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/tv_text_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="默认文字"
android:textColor="#00ff00"
android:textSize="90sp" />
</LinearLayout>
隐式Intent是在intent中指定激活组件的条件,程序会自动寻找最匹配的组件,然后进行跳转
运行结果:


简单的传值
AndroidManiFest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.lixyz.activitymanager"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"> </activity>
</application> </manifest>
MainActivity.java
package cn.lixyz.activitymanager; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText; public class MainActivity extends AppCompatActivity { Intent intent; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(MainActivity.this, SecondActivity.class);
String str = ((EditText)findViewById(R.id.ed_input)).getText().toString();
intent.putExtra("data",str); startActivity(intent);
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"> <EditText
android:id="@+id/ed_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您想要传递的值"/> <Button
android:id="@+id/bt_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/> </LinearLayout>
SecondActivity.java
package cn.lixyz.activitymanager; import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView; import org.w3c.dom.Text; /**
* Created by LGB on 2015/8/27.
*/
public class SecondActivity extends Activity { TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); String str = getIntent().getStringExtra("data"); ((TextView)findViewById(R.id.tv_text_show)).setText(str);
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/tv_text_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="默认文字"
android:textColor="#00ff00"
android:textSize="90sp" />
</LinearLayout>
运行结果:


通过代码可以看出,MainActivity使用putExtra(xx,yy)方法将yy传递给SecondActivity,而SecondActivity通过getStringExtra(xx)来接受传递过来的值
传入多个值
方法一:继续使用Intent
AndroidManiFest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.lixyz.activitymanager"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
MainActivity.java
package cn.lixyz.activitymanager; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("string","hello world");
intent.putExtra("int",104);
startActivity(intent);
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"> <EditText
android:id="@+id/ed_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您想要传递的值"/> <Button
android:id="@+id/bt_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/> </LinearLayout>
SecondActivity.java
package cn.lixyz.activitymanager; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; /**
* Created by LGB on 2015/8/27.
*/
public class SecondActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); Intent intent = getIntent();
int i = intent.getIntExtra("int",555);
String str = intent.getStringExtra("string"); ((TextView) findViewById(R.id.tv_text_show)).setText(i + "");
((TextView) findViewById(R.id.tv_text_2)).setText(str);
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/tv_text_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="默认文字"
android:textColor="#00ff00"
android:textSize="90sp" /> <TextView
android:id="@+id/tv_text_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#00ff00"
android:textSize="90sp"/>
</LinearLayout>
方法二:使用Bundle
AndroidManiFest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.lixyz.activitymanager"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
MainActivity.java
package cn.lixyz.activitymanager; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putInt("int", 105);
b.putString("string", "hello world"); intent.putExtras(b);
startActivity(intent);
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"> <EditText
android:id="@+id/ed_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您想要传递的值"/> <Button
android:id="@+id/bt_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/> </LinearLayout>
SecondActivity.java
package cn.lixyz.activitymanager; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; /**
* Created by LGB on 2015/8/27.
*/
public class SecondActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); Intent intent = getIntent();
Bundle b = intent.getExtras();
String str = b.getString("string");
int i = b.getInt("int"); ((TextView) findViewById(R.id.tv_text_show)).setText(i + "");
((TextView) findViewById(R.id.tv_text_2)).setText(str);
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/tv_text_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="默认文字"
android:textColor="#00ff00"
android:textSize="90sp" /> <TextView
android:id="@+id/tv_text_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#00ff00"
android:textSize="90sp"/>
</LinearLayout>
运行结果:


从代码可以看出,传入多个值可以直接在intent中put各种数据,也可以将数据打包成一个Bundle,然后将Bundle传入到新的Activity,新Activity接收到Bundle之后,可以从Bundle中get到打包的数据。
传入一个对象
方法一:Serializable方式
AndroidManiFest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.lixyz.activitymanager"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
MainActivity.java
package cn.lixyz.activitymanager; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class); Person person = new Person();
person.setAge(20);
person.setName("张三"); intent.putExtra("person", person); startActivity(intent);
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"> <EditText
android:id="@+id/ed_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您想要传递的值"/> <Button
android:id="@+id/bt_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/> </LinearLayout>
SecondActivity.java
package cn.lixyz.activitymanager; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; /**
* Created by LGB on 2015/8/27.
*/
public class SecondActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); Intent intent = getIntent();
Person person = (Person) intent.getSerializableExtra("person");
String name = person.getName();
int age = person.getAge(); ((TextView) findViewById(R.id.tv_text_show)).setText(age + "");
((TextView) findViewById(R.id.tv_text_2)).setText(name);
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/tv_text_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="默认文字"
android:textColor="#00ff00"
android:textSize="90sp" /> <TextView
android:id="@+id/tv_text_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#00ff00"
android:textSize="90sp"/>
</LinearLayout>
Person.java
package cn.lixyz.activitymanager; import java.io.Serializable; /**
* Created by LGB on 2015/8/28.
*/
public class Person implements Serializable { public Person() { } private String name;
private int age; public String getName() {
return name;
} public int getAge() {
return age;
} public void setName(String name) {
this.name = name;
} public void setAge(int age) {
this.age = age;
}
}
运行结果:


方法二:Parcelable方式
AndroidManiFest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.lixyz.activitymanager"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
MainActivity.java
package cn.lixyz.activitymanager; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class); Person person = new Person();
person.setAge(20);
person.setName("张三");
person.setHeight(180); Person person2 = new Person();
person2.setAge(10);
person2.setName("李四");
person2.setHeight(178); intent.putExtra("person", person);
intent.putExtra("person2",person2); startActivity(intent);
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"> <EditText
android:id="@+id/ed_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您想要传递的值"/> <Button
android:id="@+id/bt_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/> </LinearLayout>
SecondActivity.java
package cn.lixyz.activitymanager; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; /**
* Created by LGB on 2015/8/27.
*/
public class SecondActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); Intent intent = getIntent();
Person person = intent.getParcelableExtra("person");
String name = person.getName();
int age = person.getAge();
int height = person.getHeight(); Person person2 = intent.getParcelableExtra("person2");
String name2 = person2.getName();
int age2 = person2.getAge();
int height2 = person2.getHeight(); if (age2 > age) {
((TextView) findViewById(R.id.tv_text_show)).setText(age2 + "");
((TextView) findViewById(R.id.tv_text_2)).setText(name2);
((TextView) findViewById(R.id.tv_text_3)).setText(height2 + "");
}else{
((TextView) findViewById(R.id.tv_text_show)).setText(age + "");
((TextView) findViewById(R.id.tv_text_2)).setText(name);
((TextView) findViewById(R.id.tv_text_3)).setText(height + "");
}
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/tv_text_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="默认文字"
android:textColor="#00ff00"
android:textSize="90sp" /> <TextView
android:id="@+id/tv_text_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#00ff00"
android:textSize="90sp"/> </LinearLayout>
Person.java
package cn.lixyz.activitymanager; import android.os.Parcel;
import android.os.Parcelable; import java.io.Serializable; /**
* Created by LGB on 2015/8/28.
*/
public class Person implements Parcelable { private String name;
private int height;
private int age; public static final Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel in) {
Person person = new Person();
person.name = in.readString();
person.age = in.readInt();
person.height = in.readInt();
return person;
} @Override
public Person[] newArray(int size) {
return new Person[size];
}
}; public String getName() {
return name;
} public int getAge() {
return age;
} public void setName(String name) {
this.name = name;
} public void setAge(int age) {
this.age = age;
} public int getHeight() {
return height;
} public void setHeight(int height) {
this.height = height;
} @Override
public int describeContents() {
return 0;
} @Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
dest.writeInt(height);
}
}
运行结果


注意Person.java中红色代码的部分
Activity中的数据回传
在Activity的跳转中,我们往往需要在关闭这个Activity之后向上一个Activity回传数据,但使用startActivity无法达到这个要求,Android提供了startActivityForResult方法来帮我们完成这一需求。
AndroidManiFest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.lixyz.activitymanager"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
MainActivity.java
package cn.lixyz.activitymanager; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivityForResult(intent, 1);
}
});
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
String str = data.getStringExtra("string");
((TextView) findViewById(R.id.tv_text_show2)).setText(str);
}
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"> <EditText
android:id="@+id/ed_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您想要传递的值"/> <Button
android:id="@+id/bt_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
/> <TextView
android:id="@+id/tv_text_show2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#00ee00"
android:textSize="30sp"/> </LinearLayout>
SecondActivity.java
package cn.lixyz.activitymanager; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View; /**
* Created by LGB on 2015/8/27.
*/
public class SecondActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); findViewById(R.id.bt_cloBack).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("string", "hello world");
setResult(RESULT_OK, intent);
finish();
}
}); }
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/tv_text_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="默认文字"
android:textColor="#00ff00"
android:textSize="90sp" /> <Button
android:id="@+id/bt_cloBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关闭并返回"/> </LinearLayout>
运行结果:



从代码中可以看出
1. 我们使用startActivityForResult(intent, 1)来启动了SecondActivity,其中1是一个请求码,可以是任意数字,只要是唯一值即可
2. 我们在SecondActivity中构建了一个用来传递主句的Intent,然后调用了setResult()方法,该方法专门用于向上一个活动返回数据,该方法的第一个参数是向上一个活动返回处理结果,第二个参数是返回的数据
3. 由于我们是使用startActivityForResult来启动的第二个Activity,那么在第二个Activity销毁之后会调用上一个活动的onActivityResult()方法,所以我们重写该方法用来得到返回的数据
Android笔记(二十) Activity中的跳转和值传递的更多相关文章
- Java学习笔记二十:Java中的内部类
Java中的内部类 一:什么是内部类: (1).什么是内部类呢? 内部类( Inner Class )就是定义在另外一个类里面的类.与之对应,包含内部类的类被称为外部类. (2).那为什么要将一个类定 ...
- android 学习随笔十四(页面跳转与数据传递)
1.activity 创建第二个Activity 需要在清单文件中为其配置一个activity标签 标签中如果带有这个子节点,则会在系统中多创建一个快捷图标 <intent-filter> ...
- Android笔记二十四.Android基于回调的事件处理机制
假设说事件监听机制是一种托付式的事件处理,那么回调机制则与之相反,对于基于回调的事件处理模型来说,事件源和事件监听器是统一的,或者说事件监听器全然消失了,当用户在GUI控件上激发某个事件时,控 ...
- android笔记5——同一个Activity中Fragment的切换
今天来模拟一个注冊的界面过程: 点击"下一步"之后: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZW5zb24xNjg1NQ==/f ...
- python3.4学习笔记(二十二) python 在字符串里面插入指定分割符,将list中的字符转为数字
python3.4学习笔记(二十二) python 在字符串里面插入指定分割符,将list中的字符转为数字在字符串里面插入指定分割符的方法,先把字符串变成list然后用join方法变成字符串str=' ...
- Android进阶(二十八)上下文菜单ContextMenu使用案例
上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等 ...
- Android进阶(二十)AndroidAPP开发问题汇总(四)
· Android进阶(二十)AndroidAPP开发问题汇总(四) android:layout_width和android:width的区别 基中的android:layout_width和and ...
- python3.4学习笔记(二十) python strip()函数 去空格\n\r\t函数的用法
python3.4学习笔记(二十) python strip()函数 去空格\n\r\t函数的用法 在Python中字符串处理函数里有三个去空格(包括'\n', '\r', '\t', ' ')的函数 ...
- python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码
python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...
随机推荐
- python locust--Setups, Teardowns, on_start, and on_stop .
创建一个locust测试脚本,如下: from locust import HttpLocust, TaskSet, task class UserBehavior(TaskSet): def set ...
- PAT 甲级 1067 Sort with Swap(0, i) (25 分)(贪心,思维题)*
1067 Sort with Swap(0, i) (25 分) Given any permutation of the numbers {0, 1, 2,..., N−1}, it is ea ...
- Spring Cloud Config 分布式配置管理 5.3
Spring Cloud Config简介 在传统的单体式应用系统中,我们通常会将配置文件和代码放在一起,但随着系统越来越大,需要实现的功能越来越多时,我们又不得不将系统升级为分布式系统,同时也会将系 ...
- 看烦了VS2012的黑白调调了吗?换
VS2012的默认深色主题的确让整个IDE看起来很有气场,而且深色的主题保护眼睛,还是蛮不错的.但是看久了也会烦啊.虽然说重要的不是IDE看起来怎么样,而是写出来的代码质量怎么样,但一个好的环境也是会 ...
- axios发post请求,后端接收不到参数的问题
axios会帮我们自动转换请求数据和响应数据 以及 自动转换JSON数据,我们的请求头转换成 Content-Type变成了application/json;charset=utf-8,然后因为我们的 ...
- 微信浏览器内建的WeixinJSBridge 实现“返回”操作
微信浏览器内建的WeixinJSBridge 实现“返回”操作 WeixinJSBridge.call('closeWindow');
- 使用TypeScript创建Vue项目
Vue的灵活性总是让代码看起来非常洗练,对TypeScript来说也是一种挑战, 好在Vue对TypeScript进行了一次全方位的适配. 相对于React严谨的代码,Redux啰嗦的样板代码,Vue ...
- vim 常用命令总结(排版精良,内容优质)
1. 格式说明 <xxx>:尖括号的含义表示这是一个占位参数,也就是必须有的参数,实际输入的内容是 xxx [xxx]:方括号的含义表示这是一个可选参数,也就是可有可无,实际输入的内容是 ...
- C之typedef应用
1.0关于typedef关键字的基础: https://www.cnblogs.com/anSn/p/8783347.html 1.1 typedef 修饰“函数类型” 的调用方法: 1)我们写一段普 ...
- yzoj 2377 颂芬梭哈 题解
题意 Alice 和 Mukyu 最近偶然得到了一本写有一种叫做梭哈的扑克游戏的规则的说明书(名为<C████████nd>,中间部分被涂掉了),据其所述,梭哈是一种使用黑桃.红心.梅花. ...