Java 由于 Android getFragmentManager() API 已弃用,是否还有其他选择?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51737667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Since the Android getFragmentManager() API is deprecated, is there any alternative?
提问by Zubair Rajput
FragmentManager
is deprecated. Is there an alternative or what can I do now?
FragmentManager
已弃用。是否有替代方案或我现在可以做什么?
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
destination = place.getName().toString();
destinationLatLng = place.getLatLng();
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
}
});
回答by Vikasdeep Singh
Use Support Libraryovercome this issue.
使用支持库克服了这个问题。
As per documentationFragment class has been deprecated:
根据文档Fragment 类已被弃用:
This class was deprecated in API level 28. Use the Support Library Fragment for consistent behavior across all devices and access to Lifecycle.
此类在 API 级别 28 中已弃用。使用支持库片段在所有设备上保持一致的行为并访问生命周期。
回答by midhunhk
You should use the Fragment class from Android Support library instead.
您应该改用 Android 支持库中的 Fragment 类。
android.app.Fragmentwas deprecated in API level 28. Use the Support Library Fragment for consistent behavior across all devices and access to Lifecycle.
android.app.Fragment在 API 级别 28 中已弃用。使用支持库 Fragment 在所有设备上实现一致的行为并访问生命周期。
Use android.support.v4.app.Fragmentinstead.
回答by faskN
This method was deprecated in API level 28.
此方法在 API 级别 28 中已弃用。
Use FragmentActivity.getSupportFragmentManager()
用 FragmentActivity.getSupportFragmentManager()
回答by SIMMORSAL
Since the question is a little broad, here's the solution for those who have migrated to androidX
.
由于这个问题有点宽泛,这里是那些已迁移到androidX
.
Make sure that your fragments are using androidX versions in import
section. i.e if you're using DialogFragment()
, replace
确保您的片段在import
部分中使用 androidX 版本。即如果您正在使用DialogFragment()
,请更换
import android.app.DialogFragment
with
和
import androidx.fragment.app.DialogFragment
Then for fragmentManager
when show()
ing the dialog, if launching from another fragment
don't change it, but if passing from an Activity
, first extend the activity from FragmentActivity()
, then pass supportFragmentManager
:
然后fragmentManager
在show()
打开对话框时,如果从另一个启动fragment
不要改变它,但如果从一个传递Activity
,首先从 扩展活动FragmentActivity()
,然后传递supportFragmentManager
:
// kotlin
import androidx.fragment.app.FragmentActivity
class MyActivity: FragmentActivity() {
...
val myDialog = MyDialog()
myDialog.show(supportFragmentManager, TAG)
...
}
Althoghthe same steps also apply to support.v4
if you've not migrated to androidX
. Just in the first step replace
Althogh相同的步骤也适用于support.v4
如果您还没有迁移到androidX
。只是在第一步替换
import android.app.DialogFragment
with
和
import android.support.v4.app.Fragment
回答by A.Klapchuk
Xml
xml
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment"
/>
Activity
活动
SupportPlaceAutocompleteFragment autocompleteFragment = (SupportPlaceAutocompleteFragment)
getSupportFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
回答by Mina Samir
If I understood you well getFragmentManager()
is now deprecated
如果我理解你getFragmentManager()
,现在已弃用
we will use getChildFragmentManager()
Return a private FragmentManager for placing and managing Fragments inside of this Fragment.
我们将使用getChildFragmentManager()
返回一个私有的 FragmentManager 来放置和管理这个 Fragment 中的 Fragment。
we will use getParentFragmentManager()
Return the FragmentManager for interacting with fragments associated with this fragment's activity.
我们将使用getParentFragmentManager()
返回 FragmentManager 与与此片段活动相关联的片段进行交互。
so, if you deal with fragments inside a fragment you will use the first one and if you deal with fragments inside an activity you will use the second one.
因此,如果您处理片段中的片段,您将使用第一个;如果您处理活动中的片段,您将使用第二个。
you can find them here package androidx.fragment.app;
你可以在这里找到它们 package androidx.fragment.app;
回答by androidStud
Extend AppCompatActivitywhich by default extends FragmentActivity, then you can use getSupportFragmentManager()which will return you the fragmentManager.
扩展默认扩展FragmentActivity 的AppCompatActivity,然后您可以使用getSupportFragmentManager()它将返回给您fragmentManager。
回答by AndroidDev
for Kotlin:
对于科特林:
activity
?.supportFragmentManager
?. <whatever needs to come after that>
回答by yadav_nkit
Use This
用这个
import android.support.v4.app.FragmentManager;
Instead of
代替
import android.app.FragmentManager;
And Use This
并使用这个
FragmentManager manager = ((YourActivity) mContext).getSupportFragmentManager();
Instead of
代替
FragmentManager manager = ((YourActivity) mContext).getFragmentManager();
回答by Zack
I used parentFragmentManager
instead of fragmentManager
.
(If you are using androidxFragment
.)
我用parentFragmentManager
而不是fragmentManager
. (如果您使用的是androidxFragment
。)
https://developer.android.com/jetpack/androidx/releases/fragment#1.2.0
https://developer.android.com/jetpack/androidx/releases/fragment#1.2.0
getFragmentManager() deprecation: The
getFragmentManager()
andrequireFragmentManager()
methods onFragment
have been deprecated and replaced with a singlegetParentFragmentManager()
method, which returns the non-nullFragmentManager
theFragment
is added to (you can useisAdded()
to determine if it is safe to call).
getFragmentManager()折旧:在
getFragmentManager()
和requireFragmentManager()
方法上Fragment
已被弃用,并用单一的替代getParentFragmentManager()
方法,该方法返回非空FragmentManager
的Fragment
加入到(你可以使用isAdded()
,以确定它是否是安全的调用)。