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 ...
随机推荐
- 实现Modbus TCP多网段客户端应用
对于Modbus TCP来说与Modbus RTU和Modbus ASCII有比较大的区别,因为它是运行于以太网链路之上,是运行于TCP/IP协议之上的一种应用层协议.在协议栈的前两个版本中,Modb ...
- 【Leetcode_easy】922. Sort Array By Parity II
problem 922. Sort Array By Parity II solution1: class Solution { public: vector<int> sortArray ...
- linux系统卡顿 性能分析
systemtrap 是一个内核开发者要掌握的工具. linux performance analysis 系统瓶颈性能分析软件
- a标签添加移除事件及开启禁用事件
一.添加移除点击事件 <script type="text/javascript" src="jquery.min.js"></script& ...
- 修改Linux服务器中的MySql密码
1.可以直接在数据库中修改,因为知道root密码,所以直接登录 mysql -uroot -p 2.查看一下数据库,修改root密码需要使用如下图所示的mysql数据库 3.通过use mysql指明 ...
- 大数据架构(PB级)
1.随着互联网快速发展,数据量的快速膨胀,我们日增3000多亿数据量,因此需要针对PB级存储.几百TB的增量数据处理架构设计 2.系统逻辑划分总图: 暂不便透露 3.系统架构图: 4.大数据计算引擎我 ...
- 《Mysql - 在Mysql服务出现瓶颈时,有哪些“饮鸩止渴”提高性能的方法?》
一:情景 - 业务高峰期,生产环境的 MySQL 压力太大,没法正常响应,需要短期内.临时性地提升一些性能. - 在业务高发时候,Mysql 服务压力过大,导致业务受损, 用户的开发负责人说,不管你用 ...
- QT 设置程序图标
1.应用窗口左上角的图标.状态栏上显示的图标用setWindowIcon()函数: 2.可执行程序的图标设置: (1).右键项目添加一个资源文件 (2).导入.ico文件图标
- Ubuntu的apt命令详解()deepin linux是在Ubuntu基础上开发的
apt-cache和apt-get是apt包的管理工具,他们根据/etc/apt/sources.list里的软件源地址列表搜索目标软件.并通过维护本地软件包列表来安装和卸载软件. 查看本机是否安装软 ...
- MGR安装记录
装好所有MySQL5.7, 打开GTID 修改my.cnf文件: ## group replication transaction_write_set_extraction = XXHASH64 ## ...