从主Activity中调用子Activity,并返回结果给主Activity

使用Intent在Activity间传递数据的方法;
1.以Sub-Activity的方式启动子Activity
调用startActivityForResult(Intent,requestCode)函数。
2.设置子Activity的返回值
调用setResult()函数设定需要返回给父Activity的数据
3. 在父Activity中获取返回值
会调用onActivityResult()函数。


//abaka -------------- 子Activity

package com.example.abaka.a2_2;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class abaka extends AppCompatActivity {

    private EditText editText,editText2;

    private TextView textView,textView2;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_abaka);

        editText = (EditText)findViewById(R.id.inputUserName);

        editText2 = (EditText)findViewById(R.id.inputpassword);

        Intent intent = getIntent();

        Button button=(Button)findViewById(R.id.button);

        Button button2=(Button)findViewById(R.id.button2);

        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                String result = editText.getText().toString();

                String result2 = editText2.getText().toString();

                Intent intent = new Intent();

                intent.putExtra("result",result);

                intent.putExtra("result2",result2);

                setResult(1001,intent);

                finish();

            }

        });

        button2.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v)

            {

                Intent intent=new Intent(abaka.this,MainActivity.class);

                startActivity(intent);

            }

        });

    }

}
//MainActivity ------------ 主Activity

package com.example.abaka.a2_2;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private EditText editText,editText2;

    private Button button;

    @Override

    protected void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        button=(Button)findViewById(R.id.button2);

        editText = (EditText) findViewById(R.id.inputUserName);

        editText2 = (EditText)findViewById(R.id.inputpassword);

        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                Intent intent=new Intent(MainActivity.this,abaka.class);

                String s1 = editText.getText().toString();

                String s2 = editText2.getText().toString();

                startActivityForResult(intent,1000);

            }

        });

    }

    @Override

    protected void onActivityResult(int requestCode,int resultCode,Intent data){

        super.onActivityResult(requestCode,resultCode,data);

        if(requestCode == 1000 && resultCode == 1001){

            String result = data.getStringExtra("result");

            String result2 = data.getStringExtra("result2");

            editText.setText(result);

            editText2.setText(result2);

        }

    }

}
//activity_abaka.xml -----------------子界面





    

    

    

    

    

    

    

    

    

    
//activity_main.xml------------主界面





    

    

    

    

    

    

真机运行结果:

在登陆界面点击注册,跳转到注册界面,在注册界面点击确认会将数据带会到登陆界面如图3,在注册界面点击取消会返回图1.
从主Activity中调用子Activity,并返回结果给主Activity_第1张图片

从主Activity中调用子Activity,并返回结果给主Activity_第2张图片
从主Activity中调用子Activity,并返回结果给主Activity_第3张图片

你可能感兴趣的:(小知识,Android)