android view ondestroy,android - Force onDestroyView on a fragment's view - Stack Overflow

I'm writing an app that loads user profiles and user's ratings for different places. The app uses fragments pervasively, and it's relatively easy to jump from a profile to a rated place.

As a user clicks a profile and gets to a rated place, they can click another profile on a rated place and go on and on.

The problem I'm having is memory related, when I'm looking at a ranked place and I click a profile, I switch from one Activity to the next. In both of these activities, after setContentView I load a fragment dynamically into layout space.

Now, as I shuttle between these activities, onSaveInstance state is almost always called, however since the Fragment displaying whatever was in the foreground before the activity switch, onDestroyView is not called.

What I would like is when onSaveInstanceState is called in these dynamic fragments, it to force onDestroyView to be called as well.

The only time onDestroyView seems to be called is when I add a Fragment to the back stack. When another activity comes to the foreground and this fragment is stopped, I'd like on DestroyView to be called as well.

The current workaround I want to implement is have an empty fragment with no view, and every time I call startActivity(Intent i), load this dummy fragment to destroy views and start the next activity. Then, when I come back, pop it off the back stack and restore the actual last fragment.

This however seems excessive. So, for a stopped fragment in a stopped activity with a new activity in front of it, how do I force it to destroy it's View?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在Android应用程序中使用bga-qrcode-zbar库,可以按照以下步骤进行操作: 1. 在项目根目录的build.gradle文件中添加以下代码: ``` allprojects { repositories { maven { url 'https://jitpack.io' } } } ``` 2. 在app模块的build.gradle文件中添加以下依赖项: ``` dependencies { implementation 'com.github.yipianfengye:zbar:1.9.8' implementation 'cn.bingoogolapple:bga-qrcode-zbar:1.2.6' } ``` 3. 在代码中使用BGAQRCodeUtil类来生成和扫描二维码。例如,以下代码将生成一个包含指定字符串的二维码并显示在ImageView中: ``` String content = "https://www.example.com"; ImageView imageView = findViewById(R.id.image_view); BGAQRCodeUtil.setImageViewQRCodeWithLogo(this, imageView, content, BitmapFactory.decodeResource(getResources(), R.drawable.logo)); ``` 希望这可以帮助你成功使用bga-qrcode-zbar库。 ### 回答2: 要在Android中使用bga-qrcode-zbar库,首先需要在项目的build.gradle文件中添加以下依赖项: ``` dependencies { implementation 'cn.bingoogolapple:bga-qrcode-zbar:latestVersion' } ``` 然后,在需要使用二维码扫描的Activity中,先在布局文件中添加一个SurfaceView,用于显示相机预览,并在代码中初始化和使用扫描功能。 1. 在布局文件中添加SurfaceView: ```xml <SurfaceView android:id="@+id/sv_qrcode" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2. 在Activity中进行初始化和使用: ```java public class QRCodeScanActivity extends AppCompatActivity implements BGAQRCodeUtil.Delegate { private SurfaceView mSurfaceView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_qrcode_scan); mSurfaceView = findViewById(R.id.sv_qrcode); // 初始化扫描工具类 BGAQRCodeUtil.setDelegate(this); } @Override protected void onResume() { super.onResume(); // 打开相机并进行预览 BGAQRCodeUtil.onResume(this); } @Override protected void onPause() { super.onPause(); // 关闭相机预览并释放资源 BGAQRCodeUtil.onPause(this); } @Override protected void onDestroy() { super.onDestroy(); // 释放资源 BGAQRCodeUtil.onDestroy(this); } // 扫描结果回调 @Override public void onScanQRCodeSuccess(String result) { // 扫描成功后的处理逻辑 } // 扫描出错回调 @Override public void onScanQRCodeOpenCameraError() { // 打开相机出错的处理逻辑 } } ``` 这样,你就可以在Android应用中使用bga-qrcode-zbar库提供的功能了。记得在AndroidManifest.xml中添加相机权限:`<uses-permission android:name="android.permission.CAMERA" />`。 ### 回答3: 在Android中引用bga-qrcode-zbar的代码需要按照以下步骤进行操作: 1. 首先,在项目的build.gradle文件中的dependencies中添加以下代码依赖: ``` implementation 'cn.bingoogolapple:bga-qrcode-zbar:2.2.8' implementation 'cn.bingoogolapple:bga-qrcode-core:2.2.8' ``` 2. 然后,在布局文件的合适位置添加BGAQRCodeCaptureView控件,例如: ``` <cn.bingoogolapple.qrcode.zbar.ZBarView android:id="@+id/zbarview" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 3. 在Activity或Fragment中,找到控件并初始化: ``` private ZBarView mZBarView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mZBarView = findViewById(R.id.zbarview); } @Override protected void onResume() { super.onResume(); mZBarView.startCamera(); mZBarView.startSpot(); } @Override protected void onPause() { super.onPause(); mZBarView.stopCamera(); } @Override protected void onDestroy() { super.onDestroy(); mZBarView.onDestroy(); } ``` 4. 最后,在AndroidManifest.xml文件中添加摄像头的权限: ``` <uses-permission android:name="android.permission.CAMERA" /> ``` 现在你可以在Android应用中使用bga-qrcode-zbar库了,它提供了扫描二维码的功能。为了更好的体验,在使用该库之前,你需要检查设备是否有摄像头,并且在需要的时候请求相机权限。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值