Activity的启动和关闭方法

Activity的启动和关闭方法

一、Activity启动其他Activity有两种方法:

//启动其他Activity
void startActivity(Intent intent);

//指定请求码启动Activity,而且程序将会等到新启动的Activity的结果(重写onActivityResult方法获取)
void startActivityForResult(Intent intent, int requestCode);

在Activity类中有这样已定义好的resultCode参数:

/** Standard activity result: operation canceled. */
public static final int RESULT_CANCELED    = 0;
/** Standard activity result: operation succeeded. */
public static final int RESULT_OK           = -1;

Android为关闭Activity提供的两个方法:

//结束当前Activity
finish();

//结束以startActivityForResult(Intent intent, int requestCode)方式启动的Activity
finish(int requestCode);

二、使用startActivityForResult返回信息:

1.使用startActivityForResult进行Activity间的数据传递

MainActivity.java:

//点击MainActivity的Button时调用
private void toSecondActivity() {
    Intent intent=new Intent(this,SecondActivity.class);

    //向Intent对象传入String类型数据
    intent.putExtra("mainActivity","MainActivity传入的数据");

    //构建Bundle对象,并传入数据
    Bundle bundle=new Bundle();
    String[] stringArray=new String[]{"hahaha","heiheihei","hehehehe","wawawawa"};
    bundle.putStringArray("stringArray",stringArray);

    //将Bundle对象传入Intent
    intent.putExtras(bundle);

    //启动activity
    startActivityForResult(intent,0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode==0&&resultCode==RESULT_OK){
        Log.d(TAG, "onActivityResult: "+data.getStringExtra("secondActivity"));
    }
}

SecondActivity.java:

    //获取启动该Activity之前的Activity对应的Intent
    Intent intent = getIntent();

    //获取Intent对象携带的String类型数据
    String value = intent.getStringExtra("mainActivity");
    Log.d(TAG, "onCreate: " + value);

    //获取Intent携带的Bundle类型数据
    Bundle bundle = intent.getExtras();
    String[] array = bundle.getStringArray("stringArray");
    for (int i = 0; i < array.length; i++) {
        Log.d(TAG, "onCreate: " + array[i]);
    }


    Button btnFinish = (Button) findViewById(R.id.btn_finish);

    btnFinish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //传入字符串信息到Intent
            intent.putExtra("secondActivity", "SecondActivity传入的信息");

            //设置该SecondActivity的结果码,并设置结束之后退回的Activity
            setResult(RESULT_OK, intent);

            //结束SecondActivity
            finish();
        }
    });

log日志:

/*跳转到SecondActivity*/
SecondActivity: onCreate: MainActivity传入的数据
SecondActivity: onCreate: hahaha
SecondActivity: onCreate: heiheihei
SecondActivity: onCreate: hehehehe
SecondActivity: onCreate: wawawawa
/*SecondActivity被销毁*/
MainActivity: onActivityResult: SecondActivity传入的信息

2.使用相机拍照返回图片:

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent,REQUEST_FLAG_TAKE_PHOTO);

private final int REQUEST_FLAG_TAKE_PHOTO = 101;;//自定义

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_FLAG_TAKE_PHOTO) {
        if (resultCode == RESULT_OK) {
            //operation succeeded
            if (data.getData() != null || data.getExtras() != null) { //防止没有返回结果
                Uri uri = data.getData();
                if (uri != null) {
                    //拿到图片
                    photo = BitmapFactory.decodeFile(uri.getPath());
                }
                if (photo == null) {
                    Bundle bundle = data.getExtras();
                    if (bundle != null) {
                        photo = (Bitmap) bundle.get("data");
                        ivPhoto.setImageBitmap(photo);
                    } else {
                        Toast.makeText(getApplicationContext(), "找不到图片", Toast.LENGTH_SHORT).show();
                    }
                }
                //处理图片
                //裁剪图片
            }
            Log.d(TAG, "SecondActivity: operation succeeded");
        } else if (resultCode == RESULT_CANCELED) {
            //operation canceled
            Log.d(TAG, "SecondActivity: operation canceled");
        }
    }
}

你可能感兴趣的:(activity,数据传递)