Accessing the Activity from a Fragment

前端 未结 2 568
逝去的感伤
逝去的感伤 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: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.

提交回复
热议问题