Android(java)学习笔记73:Intent启动Activity

1. Intent启动Activity案例

(1)首先是main.xml和other.xml文件如下:

main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="This is MainActivity!"
        />
    <Button  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="OpenOtherActivity!"
        android:id="@+id/btnOpen"
        />
    <Button  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="HideActivity"
         android:id="@+id/btnHideActivity"
        />
    <Button  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="ExitActivity"
         android:id="@+id/btnExitActivity"
        />
</LinearLayout>

接着是other.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/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

(2)MainActivity.java 和 OtherActivity.java文件:

MainActivity.java:

package com.openother;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
    //声明按钮
    private Button btnOpen, btnHideActivity, btnExitActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //实例按钮
        btnOpen = (Button) findViewById(R.id.btnOpen);
        btnHideActivity = (Button) findViewById(R.id.btnHideActivity);
        btnExitActivity = (Button) findViewById(R.id.btnExitActivity);
        //给每个按钮添加监听
        btnOpen.setOnClickListener(this);
        btnHideActivity.setOnClickListener(this);
        btnExitActivity.setOnClickListener(this);
    }

    public void onClick(View v) {
        if (v == btnOpen) {    
            //创建一个意图,并且设置需打开的Activity
            Intent intent = new Intent(MainActivity.this, OtherActivity.class);
            //发送数据 
            intent.putExtra("Main", "我是发送的数据~娃哈哈");
            //启动另外一个Activity
            this.startActivity(intent);
        } else if (v == btnHideActivity) {
            this.finish();//退出Activity
        }else if (v == btnExitActivity) {
            System.exit(0);//退出程序
        }
    }}

其次是OtherActivity.java:

/**
 * 
 */
package com.openother;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

/**
 * @author Himi
 *
 */
public class OtherActivity extends Activity {
    private TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other);//设置当前的OtherActivity的视图内容绑定R.layout.other的布局文件
        tv = (TextView)findViewById(R.id.textView1);
        tv.setText("爱一个人好难");
        
        AssetManager mgr = getAssets();//AssetManager---管理应用程序的资产文件,在项目工程的assets文件夹下:
        Typeface tf = Typeface.createFromAsset(mgr, "fonts/DroidSansFallback.ttf");
        tv.setTypeface(tf);
    }
}

2. 总结:

(1)在开发软件的过程中,两个activity之间的通讯可以通过bundle类来实现,它类似于Map,用于存放key-value名值对形式的值。遇到过这样一种情况,就是没有create bundle,但是当你使用intent.putExtra之后,在另一个被call的activity中,会有bundle被传递过去,
原因就是因为intent.putExtra时,系统会检测有没有bundle,如果没有,则新建一个。所以下面这两个语句的等效的:

Intent intent = new Intent(this,xxx.class);
intent.putExtra("test", true);

startActivity(intent);

和:

Intent intent = new Intent(this,xxx.class);
Bundle bundle = new Bundle();
bundle.putBoolean("test", true);
intent.putExtras(bundle);
startActivity(intent);

这里的原因是我们可以追踪到putExtras()方法的底层如下:

public Intent putExtra(String name, String value) {
        if (mExtras == null) {
            mExtras = new Bundle();
        }
        mExtras.putString(name, value);
        return this;
    }
原文地址:https://www.cnblogs.com/hebao0514/p/4679506.html