0

I have 3 pages

  • page A - lists contacts
  • page B - pops up on clicking a notification
  • page C - pops up after clicking snooze on page B

I had this post which technically as asked works but now I have gained more clarity resulting in a follow up question.

Desired back button behavior

  • User clicks notifciation
  • user lands on page B and clicks snooze
  • user clicks back button (This broke with my impl of the answer in the other post)
  • DESIRED: user lands on page B so he can choose other

Desired behavior if user is on page A when notification is clicked

  • User clicks notification
  • user lands on page B and clicks snooze
  • user lands on page C and clicks 10 minutes
  • DESIRED: both page B and C 'finish()' and land on page A

Desired behavior if user is not in my app at all

  • user lands on page B and clicks snooze
  • user lands on page C and clicks 10 minutes
  • DESIRED: both page B and C 'finish()' and land back on whatever app they were in

After the answer to that question, I modified my code to call finish on button click on page B BUT this makes the page disappear. I think I need to delay calling finish on page B. I think I want when the user clicks 10 minutes button on page C..

finish();
previousActivity.finish();

Is this the wrong way to do it? AND how to get the previous Activity so I can call finish on it to the next activity so he can call it? or is my thinking all wrong?

EDIT: I did find this post but it is very cryptic having only been into android for a week now. Anyone have links on tutorials on this stuff? Perhaps I need a book for a deeper dive.

Dean Hiller
  • 17,183
  • 19
  • 103
  • 176

1 Answers1

1

The easiest way to do that is you should use startActivityForResult from B -> C

Case 1: you don't need handle

Case 2 & 3: you handle ten minutes like below

tenMinutes.setOnClickListener(new View.OnClickListener()
   {
        @Override
        public void onClick(View v)
        {
            Intent returnIntent = new Intent();
            returnIntent.putExtra("result", 1);
            setResult(Activity.RESULT_OK,returnIntent);
            finish();
        }
    });

now in B :

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE) {
            if(resultCode == Activity.RESULT_OK){
                int result = data.getIntExtra("result", 0);
                if (result == 1)
                    finish();
            }
        }
    }

You also can use others solution example : use fragment or Dialog for page C etc

Uuu Uuu
  • 1,200
  • 2
  • 8
  • 17
  • I just read stumbled upon affinity groups and that seemed to do the trick by just tying page B and page C to a new affinity group and calling this.finishAffinity(). Is that incorrect or bad use? Man, it's hard when you have no idea what to google. How does affinity groups compare to the above as well? I see your solution can be for a use case though where I want to re-use page B in other context(ie. I like it for that use case). I don't happen to want to re-use B yet but I see the use of your code!!! thankyou!!!! – Dean Hiller Jul 30 '20 at 14:55
  • It is correct. but it is complicate for beginner to handle, you have to know about task and back stack. I think my answer can handle more and easier than using affinity – Uuu Uuu Jul 30 '20 at 15:10
  • 听你说话很像一个中国人? 我是不是对的? I am guessing and if I am right, which city/province? – Dean Hiller Jul 31 '20 at 04:11
  • I'm from VietNam – Uuu Uuu Jul 31 '20 at 04:27