简体   繁体   中英

StartActivityForResult go back

I am using StartActivityForResult for multiple activities. My main activity is where I initialize it. On my second activity I input some values and pass to a third activity. Now, When i'm on the third activity I want to be able to go back to the second activity if ever I want to edit the values i passed. What should I do?

MainAct.java

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

    if (requestCode == REQUEST_CODE)
    {
        if (resultCode == Activity.RESULT_OK)
        {
            //Something
        }
    }

SecondAct.java

 Intent vd2 = new Intent(ViolatorDetails1.this,ViolatorDetails2.class);
                vd2.putExtra("name",name);
                vd2.putExtra("lname",lname);
                vd2.putExtra("lnumber",lnumber);
                vd2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                vd2.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
                startActivity(vd2);
                finish();

ThirdAct.java

 Intent intent = new Intent();
            intent.putExtra("firstname",name);
            intent.putExtra("lastname", lname);
            intent.putExtra("licensenumber", lnumber);
            setResult(Activity.RESULT_OK, intent);
            finish();

How can I go back to second Activity from third activity to edit some values if ever?

You should not call finish() on second activity when starting the third one.

Then onActivityResult() will be call when third activity is finished.

Call

startActivityForResult(vd2);

instead of

startActivity(vd2);

simply remove finish();

 Intent vd2 = new Intent(ViolatorDetails1.this,ViolatorDetails2.class);
                vd2.putExtra("name",name);
                vd2.putExtra("lname",lname);
                vd2.putExtra("lnumber",lnumber);
                vd2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                vd2.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
                startActivity(vd2);
                finish(); //remove this line

in this way when your third activity closes user will go back to 2nd one, also you should implement onActivityResult in your 2nd activity so you can handle weather user wants to edit or has finished and should go back to 1st activity! (ie setting the result of the intent came from 1st activity and finish the 2nd one!)


here is what I mean in code: In your 2nd activity do this,

@override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE)
    {
        if (resultCode == Activity.RESULT_OK)
        {
           // user should have done his job on 3rd activity and we should finish the 2nd activity to go back to first activity!
        }else{
          //user still editing!
        }
    }

and instead of startActivity(vd2); do startActivityForResult(vd2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM