gpt4 book ai didi

java - 从获得许可开始 fragment

转载 作者:行者123 更新时间:2023-11-29 18:54:57 25 4
gpt4 key购买 nike

我只想在用户授予存储权限时从 Activity 中启动一个 fragment 。我的主要 Activity 布局如下所示

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/root_container"
android:fitsSystemWindows="true"
android:background="@drawable/splash_gradient">
</FrameLayout>

现在这个布局在主要 Activity 开始时显示。我设置代码以获得存储权限。并想用我的 fragment 布局替换此布局,但只有在授予存储权限后,目前它不起作用我的 fragment 正在加载一切正常,除了我的 fragment 在授予权限之前启动。有没有办法在权限函数上设置回调方法,只有在授予权限时才启动 fragment ?

我的 onCreate 方法看起来像

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.root_container);
boolean b = getPermission();
if(b ==true){
HomeFragment fragment = new HomeFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.root_container, fragment).commit();
}
else getPermission();
}

我的获取权限方法如下所示

    public boolean getPermission() {
boolean f = false;
//Check for SDK Version if greater than 23 then seek user permission
if(Build.VERSION.SDK_INT>=23) {
//Check whether permission is already granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Ask for user permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 404);
if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!=PackageManager.PERMISSION_DENIED)
f = true;
}
else f = true;
}
return f;
}

当我授予权限时,getPermission 返回 true,但当应用程序首次运行时,它甚至在获得 getPermission() 方法的结果之前就启动了 fragment ,当我第二次运行应用程序时,它显示了 fragment 布局。

最佳答案

您应该使用 onRequestPermissionsResult 来处理结果:

private static final int REQUEST_CODE = 404;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
// Your code

if (savedInstanceState == null) {
tryToOpenHomeFragment();
}
}

private void tryToOpenHomeFragment() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
showHomeFragment();
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
showHomeFragment();
} else {
// Permission was not granted
}
}
}

private void showHomeFragment() {
HomeFragment fragment = new HomeFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.root_container, fragment)
.commit();
}

关于java - 从获得许可开始 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50067149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com