Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

android - How to manage activity with startActivityForResult

I got a situation like this:

Activity A --> B --> C --> D, when D finished, I have to refresh ActivityA to display what I input in ActivityB , ActivityC and ActivityD.Here is my code:

ActivityA

    @OnClick(R.id.btn_one)
    public void toActivityB(){
        Intent intent = new Intent();
        intent.setClass(this, ActivityB.class);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK){
            String b = data.getStringExtra("b");
            String c = data.getStringExtra("c");
            String d = data.getStringExtra("d");
            tvAll.setText(etOne.getText().toString() + "
" + b + "
" + c + "
" + d);
        }
    }

ActivityB

    @OnClick(R.id.btn_two)
    public void toActivityC(){
        Intent intent = new Intent();
        intent.setClass(this, ActivityC.class);
        startActivityForResult(intent, 2);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2 && resultCode == RESULT_OK){
            data.putExtra("b", etTwo.getText().toString());
            setResult(RESULT_OK, data);
            finish();
        }
    }

ActivityC code just like ActivityB, the last ActivityD as follow:

    @OnClick(R.id.btn_four)
    public void returnToActivityA(){
        Intent intent = new Intent();
        intent.putExtra("d", etFour.getText().toString());
        setResult(RESULT_OK, intent);
        finish();
    }

In this way, I can get the input value in ActivityB,ActivityC,ActivityD.but I have to override the method onActivityResult at every Activity and handle the data one by one.Is there any another easy way for me to handle this situation?Any good suggestion I'll be appreciate.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would suggest you to use a single activity and use fragments as forms to enter the data and once all the data is collected you can either save it in shared preference or SQLITE database depending upon the data to be saved then start a new activity to display the data stored
OR
create a parceleble object and send this object to new Activity and display it there.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...