To call getSupportFragmentManager in Activity

Asked 2 years ago, Updated 2 years ago, 112 views

I would like to call getSupportFragmentManager for a class that does not inherit FragmentActivity (inheriting Activity).


Because you still need to see dialogs in previous operating systems You cannot use getFragmentManager.

import android.support.v4.app.FragmentActivity;

I'm using this as an alternative.

試Trial 】
·Cast from Activity to fragmentActivity

this.fragmentActivity=(FragmentActivity)activity;

We failed and got this error.

Causeed by: java.lang.ClassCastException: ○○.MainActivity

Is it impossible after all?I would appreciate it if you could give me a hint.

android fragment dialog

2022-09-30 16:45

1 Answers

In a class that does not inherit FragmentActivity (inheriting Activity),

Instead of choosing to inherit Activity and casting what was Activity on top of it into FragmentActivity, use the original class inherited from FragmentActivity.All you have to do is call getSupportFragmentManager() in that class.

In other words, if you rewrite the class itself as extendsFragmentActivity, you can call getSupportFragmentManager() to get the android.support.v4.app.FragmentManager object

.


2022-09-30 16:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.