1

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?

Ekko Sky
  • 39
  • 1
  • 1
  • 10
  • If this is for something like a multi-page form, it would be way easier to go with one Activity and some fragments or different views. – Ridcully Aug 20 '17 at 14:51

2 Answers2

0

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);
smora
  • 717
  • 5
  • 18
  • I'm sorry. Silly question, but what do you mean with the onActivityResult()? Wheres should I call it? cuz I already did on the first activity. – Ekko Sky Aug 20 '17 at 14:53
  • onActivityResult of activity(A) is called when activity(B) that your start from activity(A) with startActivityForResult () is finished. If you start another activity(C) from activity(B), then you should implement onActivityResult in Activity(B) to detect Activity(C) finish. hehe not so easy to explain ! – smora Aug 20 '17 at 15:00
  • And this is android who automatically call onActivityResult(), not you. You have just to override if you need. – smora Aug 20 '17 at 15:06
  • @smora Please read [this](https://stackoverflow.com/questions/14119992/difference-between-startactivityforresult-and-startactivity) before giving an answer. – UmarZaii Aug 20 '17 at 15:17
  • @UmarZaii ok thanks but I dont get it. You know I try to do my best in answering this, you are free to make your own answer if you think mine is not good and I will be happy to learn from too. – smora Aug 20 '17 at 15:25
0

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! (i.e. 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);

Atef Hares
  • 4,715
  • 3
  • 29
  • 61
  • What I want to do is when I'm on third activity when the user clicks the back button I want to be able to go back to the second activity and edit a few edittexts. But when the user clicks the button on the third activity it should go back to the first activity to execute some methods. I have done what you said to put on activity two but nothing happens. – Ekko Sky Aug 20 '17 at 15:34
  • `protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE) { if (resultCode == Activity.RESULT_OK) { Intent intent = new Intent(); setResult(Activity.RESULT_OK, intent); finish(); } ` codes I put on 2nd activity – Ekko Sky Aug 20 '17 at 15:34
  • @EkkoSky If you followed my code correctly you should have two cases now to handle! in both cases user will press backbtn on 3rd activity and go back to 2nd activity. but in 2nd activity, in `onActivityResult` you should decide whether to stay on the 2nd activity to edit `edittexts` or go back to 1st activity and execute methods! – Atef Hares Aug 20 '17 at 15:39
  • @EkkoSky plz explain your problem in more details after reading my 1st comment ! – Atef Hares Aug 20 '17 at 15:40