Reputation: 18308
My activity extends AppCompatActivity
, and I started a Fragment
there with support library fragment manager:
public MyActivity extends AppCompatActivity {
...
@Override
public void onResume() {
super.onResume();
// show my fragment
getSupportFragmentManager().beginTransaction().add(android.R.id.content, myFragment, TAG).commit();
}
}
My question is, in MyFragment
, how can I get the instance of MyActivity
who started it?
//NOTE: it extends android.support.v4.app.Fragment
public class MyFragment extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// How to get the instance of MyActivity here?
// I tried: getActivity() but it returns FragmentActivity
//& MyActivity is AppCompatActivity
}
}
I know there is getActivity()
function in Fragment
, but it returns FragmentActivity
:
Upvotes: 1
Views: 1265
Reputation: 11457
Try this
((MyActivity) getActivity()).
then access anything from activity class
Upvotes: 1