Search code examples
androidandroid-activityandroid-lifecycle

How to get Activity from which second Activity was started?


I have activity A, MyRecyclerAdapter, and activity B.

In activity A I build MyRecyclerAdapter and start new activity B from recycler item click.

Now I need to access activity A in onDestroy method from activity B. How can I do it?

Update: I tried:

ActivityA parent = (ActivityA) getParent();
parent.setRead(id);

But it gives me null. I think it's because A is not a direct child of B;


Solution

  • For this purpose you should use startActivityForResult(intent). Then you override onActivityResult() in activity A to handle the data you receive after activity B was destroyed. In onDestroy() you'd just have to set the result with setResult(resultCode, data).

    Like this you don't need to know about activity A in activity B.