Activity切换(带参数)

  关于Activity在这里我只谈携带参数的切换。在带参数的Activity切换的时候主要有以下三个函数:

  • startActivityForResult(Intent,requestCode)
  • onActivityResult(int requestCode,int resultCode,Intent data)
  • setResult(RESULT_OK,Intent)

首先解释以下这三个函数:

1.startActivityForResult(Intent,requestCode)

  在这个函数中有两个参数,Intent和requestCode,Intent不过多解释,主要是requestCode这个参数在Activity结束时归还在onActivityResult中,以便确定的是哪个Activity返回的。

2.onActivityResult(int requestCode,int resultCode,Intent data)

  这个函数有三个参数,第一个就是requestCode来确定返回的是哪个Activity,第二个是resultCode这个确定的是activity返回的结果是否有效,一般有RESULT_OK和RESULT_CANCELED,RESULT_OK表明返回的结果data有效,而RESULT_OK则表明返回的数据data无效。

3.setResult(RESULT_OK,Intent)

  这个函数有两个函数,第一个就死返回的resultcode 表明返回的数据是否有效,第二个就是表明的返回的数据。

下面看一个简单的例子:
MainActivity:

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

public class MainActivity extends Activity {
    private static final int other=1; //标记Activity,在函数中就是requestCode
    private TextView text1;//这里表示的是layout中的一个textview
    private TextView text2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onClick(View v){   //监听函数
        switch (v.getId()){
            case R.id.button:{
                Intent intent=new Intent(MainActivity.this,ExampleActivity.class);
                startActivityForResult(intent,other);  // Intent Requestcode
            }
        }
    }
    @Override
    public void onActivityResult(int requestCode,int resultCode,Intent data){
        switch (requestCode){    //判断返回的是哪个Acitvity
            case other:{
                if(resultCode==RESULT_OK) {
                    text1 = (TextView) findViewById(R.id.text1);
                    text2 = (TextView) findViewById(R.id.text2);
                    Bundle otherbudle = data.getExtras();
                    //这里和后面的Activity中的对应intent.putExtra("fromOther",context);
                    //提取“fromOther”和“Zhang”对应的内容
                    String message1= otherbudle.getString("fromOther");
                    String message2= otherbudle.getString("Zhang");
                    text1.setText(message1);  //改变TextView中的内容
                    text2.setText(message2);
               }
            }
        }
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zhanghanlun.example.MainActivity"
    android:orientation="vertical"
    >
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="跳转Example"
        />
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text1"
        />
    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text2"
        />
</LinearLayout>

ExmapleActivity

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

/**
 * Created by zhanghanlun on 2017/4/15.
 */

public class ExampleActivity extends Activity {
    private static final int main=0;
    private TextView a=null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);

    }
    public void onClick(View v){
        switch (v.getId()){
            case R.id.button2:{
                Intent intent=new Intent(ExampleActivity.this,MainActivity.class);
                String context="zhanghanlun_Example";
                String context1="zhanghanlun_Example1";
                intent.putExtra("fromOther",context);
                intent.putExtra("Zhang",context1);
                setResult(RESULT_OK,intent);     //传递回去resultCode和data数据
                finish();                         //结束Activity
            }
        }
    }
}

activity_example.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="跳转main"/>
</LinearLayout>

最后的运行结果:
MainActivity
这里写图片描述
跳转到ExampleActivity
这里写图片描述
跳转到MainActivity
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值