如何管理结果的活动

时间:2014-08-23 08:12:21

标签: android android-intent android-activity onactivityresult

我只有2个活动。

我想从activity1转到activity2,做一些工作,然后将结果从activty2返回到activity1

这是我在activity1中的代码:

Button otherkey = (Button) findViewById(R.id.button2);
        otherkey.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent3 = new Intent(EncryptionActivity.this , FileExplore.class);
                startActivityForResult(intent3, 123);
                onActivityResult(123, Activity.RESULT_OK , intent3) ;//i think heres my problem.
            }});

并且在activity1类中我有这个方法:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        System.out.println("req" + requestCode + "\nres code :"+resultCode);
        if (requestCode == 123) {
            if(resultCode == 0){
                String result=data.getStringExtra("mydata");
                System.out.println(result);
            }
            if (resultCode == RESULT_CANCELED) {
                //Write your code if there's no result
                System.out.println("ddd");
            }
        }
    }//onActivityResult

,activity2中的代码是:

                        activity2.this.finish();
                        Intent enca = new Intent();
                        enca.putExtra("mydata", "hello");
                        setResult(RESULT_OK,enca);

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您无需致电onActivityResult方法,只需简单override即可。并且会在回到您的活动时自动调用。查看此link以了解更多

public class TestActtiviy extends Activity{

    Button width, height, calc;
    TextView area;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

     //your code
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        //your code here
         if (resultCode == 0) {}

    }

}