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 ...
随机推荐
- spring @Transactional的自调用失效问题与事务的典型错误用法剖析
@Transactional的自调用失效问题 有时候配置了注解@Transactional,但是它会失效,这里要注意一些细节问题,以避免落入陷阱. 注解@Transaction的底层实现是Spring ...
- Gson反序列json到实体类
gson在基准测试过程中各项性能接近于Jackson(具体可以看Benchmark of Java JSON libraries)里面的测试, 本人亲测过,实测结果与他的数据一致,Jackson安全性 ...
- PhpStorm文本选择范围的纵向起始位置可选
ide一般都有这个功能. Alt+Shift+insert开启这个功能,使用效果如下,红色是默认选择的范围,绿色是我们想要的
- IE11的变化 navigator.userAgent中不再包含“MSIE”关键字
IE升级了,让人好头疼,升级个东西,我们也要跟着升级,程序猿压力大呀.... 1.navigator.userAgent中不再包含“MSIE”关键字 2.用javascript的判断是否是IE11的方 ...
- Vue简单基础 + 实例 及 组件通信
vue的双向绑定原理:Object.defineProperty() vue实现数据双向绑定主要是:采用数据劫持结合发布者-订阅者模式的方式,通过 Object.defineProperty() 来劫 ...
- mysql You can't specify target table for update in FROM clause解决方法
mysql You can't specify target table for update in FROM clause解决方法出现这个错误的原因是不能在同一个sql语句中,先select同一个表 ...
- Stream知识点总结及源码阅读
上次[http://www.cnblogs.com/webor2006/p/7795596.html]对Stream进行了简单的学习,这次继续学习Stream,还是结合java8 in action这 ...
- LeetCode 605. 种花问题(Can Place Flowers) 6
605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...
- vue打包静态资源后显示空白及static文件路径报错
1.打包之后打开dist的页面显示空白: 这个是打包项目比较常见的一个错误 改一下config下面的index.js中bulid模块导出的路径.因为打包后的index.html里边的内容都是通过scr ...
- [转帖]时序数据库技术体系 – InfluxDB TSM存储引擎之数据读取
时序数据库技术体系 – InfluxDB TSM存储引擎之数据读取 http://hbasefly.com/2018/05/02/timeseries-database-7/ 2018年5月2日 ...