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之间的跳转。

运行结果:

Android笔记(二十) Activity中的跳转和值传递Android笔记(二十) 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中指定激活组件的条件,程序会自动寻找最匹配的组件,然后进行跳转

         运行结果:

Android笔记(二十) Activity中的跳转和值传递Android笔记(二十) Activity中的跳转和值传递

简单的传值

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>

运行结果:

Android笔记(二十) Activity中的跳转和值传递Android笔记(二十) Activity中的跳转和值传递

通过代码可以看出,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>

运行结果:

Android笔记(二十) Activity中的跳转和值传递Android笔记(二十) Activity中的跳转和值传递

         从代码可以看出,传入多个值可以直接在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;
}
}

运行结果:

Android笔记(二十) Activity中的跳转和值传递Android笔记(二十) Activity中的跳转和值传递

方法二: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);
}
}

运行结果

Android笔记(二十) Activity中的跳转和值传递Android笔记(二十) Activity中的跳转和值传递

注意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>

运行结果:

Android笔记(二十) Activity中的跳转和值传递

Android笔记(二十) Activity中的跳转和值传递

Android笔记(二十) Activity中的跳转和值传递

从代码中可以看出

1. 我们使用startActivityForResult(intent, 1)来启动了SecondActivity,其中1是一个请求码,可以是任意数字,只要是唯一值即可

2. 我们在SecondActivity中构建了一个用来传递主句的Intent,然后调用了setResult()方法,该方法专门用于向上一个活动返回数据,该方法的第一个参数是向上一个活动返回处理结果,第二个参数是返回的数据

3. 由于我们是使用startActivityForResult来启动的第二个Activity,那么在第二个Activity销毁之后会调用上一个活动的onActivityResult()方法,所以我们重写该方法用来得到返回的数据

上一篇:51nod1079(中国剩余定理)


下一篇:牛客网Java刷题知识点之构造函数是什么、一般函数和构造函数什么区别呢、构造函数的重载、构造函数的内存图解