我们知道,一个APP是由若干个Activity组成的,那么各个Acitivity中肯定需要进行跳转以及传递数值以保证App的运行,现总结一下多个Activity之间的跳转和值传递。

显式Intent跳转

AndroidManiFest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="cn.lixyz.activitymanager">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:theme="@style/AppTheme">
  10. <activity
  11. android:name=".MainActivity"
  12. android:label="@string/app_name">
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15.  
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. <activity android:name=".SecondActivity">
  20.  
  21. </activity>
  22. </application>
  23.  
  24. </manifest>

MainActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10.  
  11. public class MainActivity extends AppCompatActivity {
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17.  
  18. findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  22. startActivity(intent);
  23. }
  24. });
  25. }
  26. }

activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity"
  6. android:orientation="vertical">
  7.  
  8. <EditText
  9. android:id="@+id/ed_input"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:hint="请输入您想要传递的值"/>
  13.  
  14. <Button
  15. android:id="@+id/bt_submit"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:text="提交"
  19. />
  20.  
  21. </LinearLayout>

SecondActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. /**
  7. * Created by LGB on 2015/8/27.
  8. */
  9. public class SecondActivity extends Activity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_second);
  14. }
  15. }

activity_second.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6.  
  7. <TextView
  8. android:id="@+id/tv_text_show"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="默认文字"
  12. android:textColor="#00ff00"
  13. android:textSize="90sp" />
  14. </LinearLayout>

从代码中可以看出,我们在一个Activity中,使用startActivity()传入一个Intent对象来实现Activity之间的跳转。

运行结果:

隐式Intent跳转

AndroidManiFest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="cn.lixyz.activitymanager">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:theme="@style/AppTheme">
  10. <activity
  11. android:name=".MainActivity"
  12. android:label="@string/app_name">
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15.  
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. <activity android:name=".SecondActivity">
  20. <intent-filter>
  21. <action android:name="maintosecond" />
  22. <category android:name="android.intent.category.DEFAULT" />
  23. </intent-filter>
  24. </activity>
  25. </application>
  26. </manifest>

MainActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.EditText;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18.  
  19. findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. Intent intent = new Intent("maintosecond");
  23. startActivity(intent);
  24. }
  25. });
  26. }
  27. }

activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity"
  6. android:orientation="vertical">
  7.  
  8. <EditText
  9. android:id="@+id/ed_input"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:hint="请输入您想要传递的值"/>
  13.  
  14. <Button
  15. android:id="@+id/bt_submit"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:text="提交"
  19. />
  20.  
  21. </LinearLayout>

SecondActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. /**
  7. * Created by LGB on 2015/8/27.
  8. */
  9. public class SecondActivity extends Activity {
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_second);
  15. }
  16. }

activity_second.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6.  
  7. <TextView
  8. android:id="@+id/tv_text_show"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="默认文字"
  12. android:textColor="#00ff00"
  13. android:textSize="90sp" />
  14. </LinearLayout>

         隐式Intent是在intent中指定激活组件的条件,程序会自动寻找最匹配的组件,然后进行跳转

         运行结果:

简单的传值

AndroidManiFest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="cn.lixyz.activitymanager">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:theme="@style/AppTheme">
  10. <activity
  11. android:name=".MainActivity"
  12. android:label="@string/app_name">
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15.  
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. <activity android:name=".SecondActivity">
  20.  
  21. </activity>
  22. </application>
  23.  
  24. </manifest>

MainActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.EditText;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14. Intent intent;
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20.  
  21. findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View v) {
  24. intent = new Intent(MainActivity.this, SecondActivity.class);
  25. String str = ((EditText)findViewById(R.id.ed_input)).getText().toString();
  26. intent.putExtra("data",str);
  27.  
  28. startActivity(intent);
  29. }
  30. });
  31. }
  32. }

activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity"
  6. android:orientation="vertical">
  7.  
  8. <EditText
  9. android:id="@+id/ed_input"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:hint="请输入您想要传递的值"/>
  13.  
  14. <Button
  15. android:id="@+id/bt_submit"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:text="提交"
  19. />
  20.  
  21. </LinearLayout>

SecondActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.widget.TextView;
  7.  
  8. import org.w3c.dom.Text;
  9.  
  10. /**
  11. * Created by LGB on 2015/8/27.
  12. */
  13. public class SecondActivity extends Activity {
  14.  
  15. TextView textView;
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_second);
  21.  
  22. String str = getIntent().getStringExtra("data");
  23.  
  24. ((TextView)findViewById(R.id.tv_text_show)).setText(str);
  25. }
  26. }

activity_second.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6.  
  7. <TextView
  8. android:id="@+id/tv_text_show"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="默认文字"
  12. android:textColor="#00ff00"
  13. android:textSize="90sp" />
  14. </LinearLayout>

运行结果:

通过代码可以看出,MainActivity使用putExtra(xx,yy)方法将yy传递给SecondActivity,而SecondActivity通过getStringExtra(xx)来接受传递过来的值

传入多个值

方法一:继续使用Intent

AndroidManiFest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="cn.lixyz.activitymanager">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:theme="@style/AppTheme">
  10. <activity
  11. android:name=".MainActivity"
  12. android:label="@string/app_name">
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15.  
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. <activity android:name=".SecondActivity"></activity>
  20. </application>
  21. </manifest>

MainActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.EditText;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18.  
  19. findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  23.  
  24. intent.putExtra("string","hello world");
  25. intent.putExtra("int",104);
  26. startActivity(intent);
  27. }
  28. });
  29. }
  30. }

activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity"
  6. android:orientation="vertical">
  7.  
  8. <EditText
  9. android:id="@+id/ed_input"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:hint="请输入您想要传递的值"/>
  13.  
  14. <Button
  15. android:id="@+id/bt_submit"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:text="提交"
  19. />
  20.  
  21. </LinearLayout>

SecondActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.widget.TextView;
  7.  
  8. /**
  9. * Created by LGB on 2015/8/27.
  10. */
  11. public class SecondActivity extends Activity {
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_second);
  17.  
  18. Intent intent = getIntent();
  19. int i = intent.getIntExtra("int",555);
  20. String str = intent.getStringExtra("string");
  21.  
  22. ((TextView) findViewById(R.id.tv_text_show)).setText(i + "");
  23. ((TextView) findViewById(R.id.tv_text_2)).setText(str);
  24. }
  25. }

activity_second.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6.  
  7. <TextView
  8. android:id="@+id/tv_text_show"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="默认文字"
  12. android:textColor="#00ff00"
  13. android:textSize="90sp" />
  14.  
  15. <TextView
  16. android:id="@+id/tv_text_2"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:textColor="#00ff00"
  20. android:textSize="90sp"/>
  21. </LinearLayout>

方法二:使用Bundle

AndroidManiFest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="cn.lixyz.activitymanager">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:theme="@style/AppTheme">
  10. <activity
  11. android:name=".MainActivity"
  12. android:label="@string/app_name">
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15.  
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. <activity android:name=".SecondActivity"></activity>
  20. </application>
  21. </manifest>

MainActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.EditText;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18.  
  19. findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  23. Bundle b = new Bundle();
  24. b.putInt("int", 105);
  25. b.putString("string", "hello world");
  26.  
  27. intent.putExtras(b);
  28. startActivity(intent);
  29. }
  30. });
  31. }
  32. }

activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity"
  6. android:orientation="vertical">
  7.  
  8. <EditText
  9. android:id="@+id/ed_input"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:hint="请输入您想要传递的值"/>
  13.  
  14. <Button
  15. android:id="@+id/bt_submit"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:text="提交"
  19. />
  20.  
  21. </LinearLayout>

SecondActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.widget.TextView;
  7.  
  8. /**
  9. * Created by LGB on 2015/8/27.
  10. */
  11. public class SecondActivity extends Activity {
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_second);
  17.  
  18. Intent intent = getIntent();
  19. Bundle b = intent.getExtras();
  20. String str = b.getString("string");
  21. int i = b.getInt("int");
  22.  
  23. ((TextView) findViewById(R.id.tv_text_show)).setText(i + "");
  24. ((TextView) findViewById(R.id.tv_text_2)).setText(str);
  25. }
  26. }

activity_second.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6.  
  7. <TextView
  8. android:id="@+id/tv_text_show"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="默认文字"
  12. android:textColor="#00ff00"
  13. android:textSize="90sp" />
  14.  
  15. <TextView
  16. android:id="@+id/tv_text_2"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:textColor="#00ff00"
  20. android:textSize="90sp"/>
  21. </LinearLayout>

运行结果:

         从代码可以看出,传入多个值可以直接在intent中put各种数据,也可以将数据打包成一个Bundle,然后将Bundle传入到新的Activity,新Activity接收到Bundle之后,可以从Bundle中get到打包的数据。

传入一个对象

方法一:Serializable方式

AndroidManiFest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="cn.lixyz.activitymanager">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:theme="@style/AppTheme">
  10. <activity
  11. android:name=".MainActivity"
  12. android:label="@string/app_name">
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15.  
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. <activity android:name=".SecondActivity"></activity>
  20. </application>
  21. </manifest>

MainActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.EditText;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18.  
  19. findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  23.  
  24. Person person = new Person();
  25. person.setAge(20);
  26. person.setName("张三");
  27.  
  28. intent.putExtra("person", person);
  29.  
  30. startActivity(intent);
  31. }
  32. });
  33. }
  34. }

activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity"
  6. android:orientation="vertical">
  7.  
  8. <EditText
  9. android:id="@+id/ed_input"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:hint="请输入您想要传递的值"/>
  13.  
  14. <Button
  15. android:id="@+id/bt_submit"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:text="提交"
  19. />
  20.  
  21. </LinearLayout>

SecondActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.widget.TextView;
  7.  
  8. /**
  9. * Created by LGB on 2015/8/27.
  10. */
  11. public class SecondActivity extends Activity {
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_second);
  17.  
  18. Intent intent = getIntent();
  19. Person person = (Person) intent.getSerializableExtra("person");
  20. String name = person.getName();
  21. int age = person.getAge();
  22.  
  23. ((TextView) findViewById(R.id.tv_text_show)).setText(age + "");
  24. ((TextView) findViewById(R.id.tv_text_2)).setText(name);
  25. }
  26. }

activity_second.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6.  
  7. <TextView
  8. android:id="@+id/tv_text_show"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="默认文字"
  12. android:textColor="#00ff00"
  13. android:textSize="90sp" />
  14.  
  15. <TextView
  16. android:id="@+id/tv_text_2"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:textColor="#00ff00"
  20. android:textSize="90sp"/>
  21. </LinearLayout>

Person.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import java.io.Serializable;
  4.  
  5. /**
  6. * Created by LGB on 2015/8/28.
  7. */
  8. public class Person implements Serializable {
  9.  
  10. public Person() {
  11.  
  12. }
  13.  
  14. private String name;
  15. private int age;
  16.  
  17. public String getName() {
  18. return name;
  19. }
  20.  
  21. public int getAge() {
  22. return age;
  23. }
  24.  
  25. public void setName(String name) {
  26. this.name = name;
  27. }
  28.  
  29. public void setAge(int age) {
  30. this.age = age;
  31. }
  32. }

运行结果:

方法二:Parcelable方式

AndroidManiFest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="cn.lixyz.activitymanager">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:theme="@style/AppTheme">
  10. <activity
  11. android:name=".MainActivity"
  12. android:label="@string/app_name">
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15.  
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. <activity android:name=".SecondActivity"></activity>
  20. </application>
  21. </manifest>

MainActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.EditText;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18.  
  19. findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  23.  
  24. Person person = new Person();
  25. person.setAge(20);
  26. person.setName("张三");
  27. person.setHeight(180);
  28.  
  29. Person person2 = new Person();
  30. person2.setAge(10);
  31. person2.setName("李四");
  32. person2.setHeight(178);
  33.  
  34. intent.putExtra("person", person);
  35. intent.putExtra("person2",person2);
  36.  
  37. startActivity(intent);
  38. }
  39. });
  40. }
  41. }

activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity"
  6. android:orientation="vertical">
  7.  
  8. <EditText
  9. android:id="@+id/ed_input"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:hint="请输入您想要传递的值"/>
  13.  
  14. <Button
  15. android:id="@+id/bt_submit"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:text="提交"
  19. />
  20.  
  21. </LinearLayout>

SecondActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.widget.TextView;
  7.  
  8. /**
  9. * Created by LGB on 2015/8/27.
  10. */
  11. public class SecondActivity extends Activity {
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_second);
  17.  
  18. Intent intent = getIntent();
  19. Person person = intent.getParcelableExtra("person");
  20. String name = person.getName();
  21. int age = person.getAge();
  22. int height = person.getHeight();
  23.  
  24. Person person2 = intent.getParcelableExtra("person2");
  25. String name2 = person2.getName();
  26. int age2 = person2.getAge();
  27. int height2 = person2.getHeight();
  28.  
  29. if (age2 > age) {
  30. ((TextView) findViewById(R.id.tv_text_show)).setText(age2 + "");
  31. ((TextView) findViewById(R.id.tv_text_2)).setText(name2);
  32. ((TextView) findViewById(R.id.tv_text_3)).setText(height2 + "");
  33. }else{
  34. ((TextView) findViewById(R.id.tv_text_show)).setText(age + "");
  35. ((TextView) findViewById(R.id.tv_text_2)).setText(name);
  36. ((TextView) findViewById(R.id.tv_text_3)).setText(height + "");
  37. }
  38. }
  39. }

activity_second.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6.  
  7. <TextView
  8. android:id="@+id/tv_text_show"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="默认文字"
  12. android:textColor="#00ff00"
  13. android:textSize="90sp" />
  14.  
  15. <TextView
  16. android:id="@+id/tv_text_2"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:textColor="#00ff00"
  20. android:textSize="90sp"/>
  21.  
  22. </LinearLayout>

Person.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.os.Parcel;
  4. import android.os.Parcelable;
  5.  
  6. import java.io.Serializable;
  7.  
  8. /**
  9. * Created by LGB on 2015/8/28.
  10. */
  11. public class Person implements Parcelable {
  12.  
  13. private String name;
  14. private int height;
  15. private int age;
  16.  
  17. public static final Creator<Person> CREATOR = new Creator<Person>() {
  18. @Override
  19. public Person createFromParcel(Parcel in) {
  20. Person person = new Person();
  21. person.name = in.readString();
  22. person.age = in.readInt();
  23. person.height = in.readInt();
  24. return person;
  25. }
  26.  
  27. @Override
  28. public Person[] newArray(int size) {
  29. return new Person[size];
  30. }
  31. };
  32.  
  33. public String getName() {
  34. return name;
  35. }
  36.  
  37. public int getAge() {
  38. return age;
  39. }
  40.  
  41. public void setName(String name) {
  42. this.name = name;
  43. }
  44.  
  45. public void setAge(int age) {
  46. this.age = age;
  47. }
  48.  
  49. public int getHeight() {
  50. return height;
  51. }
  52.  
  53. public void setHeight(int height) {
  54. this.height = height;
  55. }
  56.  
  57. @Override
  58. public int describeContents() {
  59. return 0;
  60. }
  61.  
  62. @Override
  63. public void writeToParcel(Parcel dest, int flags) {
  64. dest.writeString(name);
  65. dest.writeInt(age);
  66. dest.writeInt(height);
  67. }
  68. }

运行结果

注意Person.java中红色代码的部分

Activity中的数据回传

在Activity的跳转中,我们往往需要在关闭这个Activity之后向上一个Activity回传数据,但使用startActivity无法达到这个要求,Android提供了startActivityForResult方法来帮我们完成这一需求。

AndroidManiFest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="cn.lixyz.activitymanager">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:theme="@style/AppTheme">
  10. <activity
  11. android:name=".MainActivity"
  12. android:label="@string/app_name">
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15.  
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. <activity android:name=".SecondActivity"></activity>
  20. </application>
  21. </manifest>

MainActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14.  
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19.  
  20. findViewById(R.id.bt_submit).setOnClickListener(new View.OnClickListener() {
  21. @Override
  22. public void onClick(View v) {
  23. Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  24.  
  25. startActivityForResult(intent, 1);
  26. }
  27. });
  28. }
  29.  
  30. @Override
  31. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  32. super.onActivityResult(requestCode, resultCode, data);
  33. switch (requestCode) {
  34. case 1:
  35. if (resultCode == RESULT_OK) {
  36. String str = data.getStringExtra("string");
  37. ((TextView) findViewById(R.id.tv_text_show2)).setText(str);
  38. }
  39. }
  40. }
  41. }

activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity"
  6. android:orientation="vertical">
  7.  
  8. <EditText
  9. android:id="@+id/ed_input"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:hint="请输入您想要传递的值"/>
  13.  
  14. <Button
  15. android:id="@+id/bt_submit"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:text="提交"
  19. />
  20.  
  21. <TextView
  22. android:id="@+id/tv_text_show2"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:textColor="#00ee00"
  26. android:textSize="30sp"/>
  27.  
  28. </LinearLayout>

SecondActivity.java

  1. package cn.lixyz.activitymanager;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7.  
  8. /**
  9. * Created by LGB on 2015/8/27.
  10. */
  11. public class SecondActivity extends Activity {
  12.  
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_second);
  17.  
  18. findViewById(R.id.bt_cloBack).setOnClickListener(new View.OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. Intent intent = new Intent();
  22. intent.putExtra("string", "hello world");
  23. setResult(RESULT_OK, intent);
  24. finish();
  25. }
  26. });
  27.  
  28. }
  29. }

activity_second.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6.  
  7. <TextView
  8. android:id="@+id/tv_text_show"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="默认文字"
  12. android:textColor="#00ff00"
  13. android:textSize="90sp" />
  14.  
  15. <Button
  16. android:id="@+id/bt_cloBack"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:text="关闭并返回"/>
  20.  
  21. </LinearLayout>

运行结果:

从代码中可以看出

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

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

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

Android笔记(二十) Activity中的跳转和值传递的更多相关文章

  1. Java学习笔记二十:Java中的内部类

    Java中的内部类 一:什么是内部类: (1).什么是内部类呢? 内部类( Inner Class )就是定义在另外一个类里面的类.与之对应,包含内部类的类被称为外部类. (2).那为什么要将一个类定 ...

  2. android 学习随笔十四(页面跳转与数据传递)

    1.activity 创建第二个Activity 需要在清单文件中为其配置一个activity标签 标签中如果带有这个子节点,则会在系统中多创建一个快捷图标 <intent-filter> ...

  3. Android笔记二十四.Android基于回调的事件处理机制

        假设说事件监听机制是一种托付式的事件处理,那么回调机制则与之相反,对于基于回调的事件处理模型来说,事件源和事件监听器是统一的,或者说事件监听器全然消失了,当用户在GUI控件上激发某个事件时,控 ...

  4. android笔记5——同一个Activity中Fragment的切换

    今天来模拟一个注冊的界面过程: 点击"下一步"之后: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZW5zb24xNjg1NQ==/f ...

  5. python3.4学习笔记(二十二) python 在字符串里面插入指定分割符,将list中的字符转为数字

    python3.4学习笔记(二十二) python 在字符串里面插入指定分割符,将list中的字符转为数字在字符串里面插入指定分割符的方法,先把字符串变成list然后用join方法变成字符串str=' ...

  6. Android进阶(二十八)上下文菜单ContextMenu使用案例

    上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等 ...

  7. Android进阶(二十)AndroidAPP开发问题汇总(四)

    · Android进阶(二十)AndroidAPP开发问题汇总(四) android:layout_width和android:width的区别 基中的android:layout_width和and ...

  8. python3.4学习笔记(二十) python strip()函数 去空格\n\r\t函数的用法

    python3.4学习笔记(二十) python strip()函数 去空格\n\r\t函数的用法 在Python中字符串处理函数里有三个去空格(包括'\n', '\r', '\t', ' ')的函数 ...

  9. python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码

    python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...

随机推荐

  1. spring @Transactional的自调用失效问题与事务的典型错误用法剖析

    @Transactional的自调用失效问题 有时候配置了注解@Transactional,但是它会失效,这里要注意一些细节问题,以避免落入陷阱. 注解@Transaction的底层实现是Spring ...

  2. Gson反序列json到实体类

    gson在基准测试过程中各项性能接近于Jackson(具体可以看Benchmark of Java JSON libraries)里面的测试, 本人亲测过,实测结果与他的数据一致,Jackson安全性 ...

  3. PhpStorm文本选择范围的纵向起始位置可选

    ide一般都有这个功能. Alt+Shift+insert开启这个功能,使用效果如下,红色是默认选择的范围,绿色是我们想要的

  4. IE11的变化 navigator.userAgent中不再包含“MSIE”关键字

    IE升级了,让人好头疼,升级个东西,我们也要跟着升级,程序猿压力大呀.... 1.navigator.userAgent中不再包含“MSIE”关键字 2.用javascript的判断是否是IE11的方 ...

  5. Vue简单基础 + 实例 及 组件通信

    vue的双向绑定原理:Object.defineProperty() vue实现数据双向绑定主要是:采用数据劫持结合发布者-订阅者模式的方式,通过 Object.defineProperty() 来劫 ...

  6. 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同一个表 ...

  7. Stream知识点总结及源码阅读

    上次[http://www.cnblogs.com/webor2006/p/7795596.html]对Stream进行了简单的学习,这次继续学习Stream,还是结合java8 in action这 ...

  8. LeetCode 605. 种花问题(Can Place Flowers) 6

    605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...

  9. vue打包静态资源后显示空白及static文件路径报错

    1.打包之后打开dist的页面显示空白: 这个是打包项目比较常见的一个错误 改一下config下面的index.js中bulid模块导出的路径.因为打包后的index.html里边的内容都是通过scr ...

  10. [转帖]时序数据库技术体系 – InfluxDB TSM存储引擎之数据读取

    时序数据库技术体系 – InfluxDB TSM存储引擎之数据读取 http://hbasefly.com/2018/05/02/timeseries-database-7/  2018年5月2日   ...