带返回值的Intent
2016-12-10 15:24
295 查看
转载请注明出处:http://blog.csdn.net/mr_leixiansheng/article/details/53558735
与一般跳转的区别
2、
实现跳转的活动和界面
跳转后的活动和界面
与一般跳转的区别
1、startActivityForResult(intent, 1);
2、
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case 1: if (resultCode == RESULT_OK) { reData = data.getStringExtra("data_return"); } } }
3、setResult(RESULT_OK, intent);
实现跳转的活动和界面
/* 带返回值的Intent */ package com.example.administrator.intent; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { String reData; //接收返回数据 TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.text_view); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, IntentActivity.class); //跳转设置 startActivityForResult(intent, 1); //带返回的跳转 requestCode = 1 } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case 1: if (resultCode == RESULT_OK) { reData = data.getStringExtra("data_return"); } } } //活动生命周期 @Override protected void onRestart() { super.onRestart(); textView.setText(reData); //显示返回值 } }
<?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:text="Button" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button" /> <TextView android:id="@+id/text_view" android:text="返回的数据" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
跳转后的活动和界面
package com.example.administrator.intent; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; /** * Created by Administrator on 2016/12/10. */ public class IntentActivity extends AppCompatActivity { String reData = "Hello World!"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.intent_layout); TextView textView = (TextView) findViewById(R.id.text_view); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(); //返回数据 intent.putExtra("data_return", reData); setResult(RESULT_OK, intent); finish(); } }); } }
<?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/button" android:text="结束" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
相关文章推荐
- Android实现intent跳转界面传递数据(2)——onActivityResult方法接收返回值
- Intent页面跳转(有、无返回值两种实例)
- android中激活一个带返回值的Intent
- 重写返回键(intent 带返回值的)
- 安卓5.0Intent打开相册选择图片无返回值问题
- 带有返回值的intent
- 2015/8/19/横、竖、满屏/消除标题、标题栏/startActivityForResult(启动带有返回值的intent)/Intent
- Intent传递值,接收返回值
- android:跳转,Intent,有无返回值
- android:跳转,Intent,有无返回值
- android的Intent返回值
- Service 的 onStartCommand(Intent, int, int) 返回值
- 官方NotePad实例学习--带附加信息并且有返回值的显式Intent
- Android_有返回值的Intent和无返回值的Intent
- Android中Intent返回值
- 简单实现Intent的普通跳转与带返回值的跳转
- ASP.Net中使用VB.Net调用存储过程并得到返回值
- 引用作为函数返回值
- android 广播大全 Intent Action 事件
- think in java 中的混型和多返回值