startActivityForResult每次发送新数据时都会显示先前数据的数据

时间:2017-07-31 05:05:07

标签: android

我有两项活动。主要活动和儿童活动。当我按下按钮时,从这里启动子活动。我想将一些数据发送回主屏幕。我使用了startActivityForResult,但它正在工作,但每当我收集数据时,它会显示以前数据的数据(即当我再次发送新数据时,以前的数据没有被清除......形式如堆栈)?如何在发送新数据时每次清除以前的数据。

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    relativeLayout = (RelativeLayout) findViewById(R.id.rl_image);

    ivLauncher = (ImageView) findViewById(R.id.iv_launcher);
    relativeLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivityForResult(new Intent(MainActivity.this, SecondActivity.class), 1002);

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1002) {
        String duty = data.getStringExtra("WHICH");
        if (duty.equalsIgnoreCase("flag")) {

            ivLauncher.setImageResource(R.drawable.ic_assistant);

        } else if (duty.equalsIgnoreCase("music")) {
            ivLauncher.setImageResource(R.drawable.ic_audiotrack);

        } else if (duty.equalsIgnoreCase("moon")) {
            ivLauncher.setImageResource(R.drawable.ic_brightness);

        }
    }
    super.onActivityResult(requestCode, resultCode, data);


}

SecondActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    rlMoon = (RelativeLayout) findViewById(R.id.rl_moon);
    rlMoon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(SecondActivity.this, MainActivity.class);
            i.putExtra("WHICH", "moon");
            setResult(1002, i);
            finish();
        }
    });
    rlMusic = (RelativeLayout) findViewById(R.id.rl_music);
    rlMusic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(SecondActivity.this, MainActivity.class);
            i.putExtra("WHICH", "music");
            setResult(1002, i);
            finish();

        }
    });
    rlFlag = (RelativeLayout) findViewById(R.id.rl_flag);
    rlFlag.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(SecondActivity.this, MainActivity.class);
            i.putExtra("WHICH", "flag");
            setResult(1002, i);
            finish();

        }
    });
}

1 个答案:

答案 0 :(得分:2)

你也可以发送这样的数据..

在儿童活动中

@Override
public void onBackPressed() {
String data = mEditText.getText();
Intent intent = new Intent();
intent.putExtra("Data", data);
setResult(resultcode, intent);
}
主要活动中的

onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
    if(resultCode == RESULT_OK) {
        String myStr=data.getStringExtra("Data");
        mTextView.setText(myStr);
    }
}
}