How to Get Hosting Activity from a View

How to get hosting Activity from a view?

I just pulled that source code from the MediaRouter in the official support library and so far it works fine:

private Activity getActivity() {
Context context = getContext();
while (context instanceof ContextWrapper) {
if (context instanceof Activity) {
return (Activity)context;
}
context = ((ContextWrapper)context).getBaseContext();
}
return null;
}

How to get Activity reference in View class?

It should be fine to just cast the context to Activity:

MyActivity myActivity = (MyActivity) getContext();

How can a custom view get access to its activity?

The getContext() method in the View class returns the context that was passed on its constructor. Usually that's the Activity you want (Activity extends Context).
So this probably works for you:

Java:

((Activity)getContext()).someMethod(...);

Kotlin:

(context as? Activity)?.someMethod(...)

how to access the properties of the host activity from a fragment dialog (kotlin)?

I think the better approach would be to use an interface or a callback, rather than having the DialogFragment access the Activity properties.

You can create an interface:

interface DialogClickListener {
fun dialogOkClicked()
fun dialogCancelClicked()
}

Make the hosting Activity implement it:

class YourActivity: AppCompatActivity(), DialogClickListener {

override fun dialogOkClicked() {

}

override fun dialogCancelClicked() {

}
}

And in your DialogFragment, call those methods in case the Activity implements them:

btnOk.setOnClickListener(){
(activity as? DialogClickListener)?.dialogOkClicked()
dismiss()

}
btnNo.setOnClickListener(){
(activity as? DialogClickListener)?.dialogCancelClicked()
dismiss()
}

This would allow you to use this DialogFragment in other places and not inside a specific hosting Activity.

How to Access the Fragment Manager (or the Hosting Activity) from the Presenter

If you are using a presenter I assume you are working with a MVP pattern. If that the case it's better to keep your code as far away from Android components (like the fragmentManager) as possible, for a better decouple.

My suggestion would be to call a method in the View (assuming the view is an Activity), and the View would needs to be known to the presenter, thus you could use and interface for the view (which you are probably using)

Something like this:

public interface MyView {
void methodToUseFragmentManager();
}

The presenter needs to be aware of it, so it could looks like this:

MyView view:
public EPGTitleRowPresenter (MyView view){
this.view = view;
}

void methodToUpdateView(){
view.methodToUseFragmentManager();
}

And finally implement that interface on your Activity:

public class MyActivity extends AppCompatActivity implements MyView{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
presenter = new EPGTitleRowPresenter(this);
}

@Override
void methodToUseFragmentManager(){
// Here you can access the FragmentManager
}
}

I've also notice that your presenter gets the Context, to have things decoupled as said above that won't be a good idea. Whenever you need to access the context call a method in the View

EDIT

If you have more than 1 presenter, let's say you have 2: the parentPresenter and childPresenter, being EPGTitleRowPresenter the child

So your parent present should look like:

MyView view:
EPGTitleRowPresenter childPresenter;
public ParentPresenter (MyView view){
this.view = view;
childPresenter = new EPGTitleRowPresenter(view);
}

and the child (EPGTitleRowPresenter) would be the same as in the original answer.

Your activity will look like:

public class MyActivity extends AppCompatActivity implements MyView{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
presenter = new ParentPresenter(this);
}

@Override
void methodToUseFragmentManager(){
// Here you can access the FragmentManager
}
}

How to access parent Activity View in Fragment

You can use

View view = getActivity().findViewById(R.id.viewid);

Quoting docs

Specifically, the fragment can access the Activity instance with
getActivity() and easily perform tasks such as find a view in the
activity layout



Related Topics



Leave a reply



Submit