Accessing the Activity from a Fragment

前端 未结 2 551
逝去的感伤
逝去的感伤 2021-01-13 04:24

I\'m trying to change the activity title from a fragment (in this case, it\'s an android.support.v4.app.Fragment). To this end, I save the activity in an attrib

相关标签:
2条回答
  • 2021-01-13 04:33

    Nowadays you can call requiredActivity() too that return FragmentActivityobject and if fragment doesn't come from an Activity, method throws a IllegalStateException

    0 讨论(0)
  • 2021-01-13 04:39

    You can access the Activity in a Fragment using getActivity(). It can be called safely as soon as onActivityCreated() was called on the Fragment. Before that, it might not be there or might not have been fully initialized yet.

    If your thread starts before that, just note the fact somewhere in your Fragment and only change the title after onActivityCreated was called.

    0 讨论(0)
提交回复
热议问题