0

I have a custom View in a parent activity that calls...

getContext().startActivity(intent);

...in it's onClickListener. In this new Activity, there is a button callback where I need to call a method in the parent activity's custom View. The same custom View that started the Activity. I tried doing it like so:

 ((CustomView) ((LinearLayout) view.getRootView().findViewById(R.id.linear_layout_amp_row))
                    .getChildAt(col)).function();

I have tried to set the parent activity as a global variable in the new Activity, but the parent is null. I tried setting it in onCreate and onAttachToWindow. I've also tried getParent and using view. I did not see a way to pass the View through the bundle.

How do you go about getting a view from a parent activity?

John Glen
  • 771
  • 7
  • 24

2 Answers2

0

You don't need to do that bro if you are extending your parent activity just because you are inheriting properties of a parent class then you can also have access to its member variables.

  • I'm not extending. I have an activity that contains a custom View. This custom view opens a new Activity. This new Activity needs to call a function contained in the custom View. – John Glen Jul 19 '20 at 19:03
  • Oh, do you mean the new Activity should extend the parent Activity? How would that work? Both activities have their own layout and variables. I only need access to the function in the parent activity. – John Glen Jul 19 '20 at 19:05
0

if you are required to call methods of fragment or activity then you can do that is this way.

Calling fragments methods from activity Calling a Fragment method from a parent Activity

Calling activity methods from fragment ((YourActivityClassName)getActivity()).yourPublicMethod();