3

I have an app that has one main activity with FrameLayout. Many fragments are created according to users' input. Every fragment has one admob banner ad using Adview.

I add fragments using the code;

getSupportFragmentManager().beginTransaction()
                           .add(R.id.container, Constants.frg1, Constants.frgTag1)
                           .addToBackStack(Constants.frgTag1)
                           .commit();

I don't want to use transaction.replace() method because I don't want my previous fragment to be recreated when backpressed that all fragments are getting data from a remote server.

So when I add new fragment a new Adview is created inside the new fragment in onCreateView method.

After get around 10 fragments my app starts to get laggy. When I dont use Adview there is no problem with the performance.

You can say that I can use only one Adview inside the MainActivity that contains the fragments but I dont want that because I have a BottomNavigationView that is visible along the application. BottomNavigationBar + Adview = very little place remains for my app content in the screen.

So is there any idea to optimize my app performance using many fragment and each has its own AdView?

Mtok
  • 1,600
  • 11
  • 36
  • 62

3 Answers3

2

After get around 10 fragments my app starts to get laggy. When I dont use Adview there is no problem with the performance.

If you are adding multiple Fragments to the backStack, your app will require more resources to store this elements, now adding an advertisement on every Fragment the result will be a high resource consumption, fast battery drainage, slow application, etc.

The use of Fragments in an application is intended to represent a portion of an UI in an Activity, in fact you will use a complete Fragment to change all the UI in an Activity.

Keeping several Fragments in the backstack is almost like keeping several Activies in the stack, this aproach is incorrect an will cause problems in your application!

How optimize your app with multiple Fragments/ads

1)Use the method onPause() of your activity to destroy the Ad and onResume() to recreate it if you are using the Activity

How to solve adview CPU consuming.

2) Implement Admob via java code:

AdMob banners cause high CPU usage

3) Enable hardware Acceleration:

Enable to your activity:

 <activity android:hardwareAccelerated="true" />

or in all your application:

<application android:hardwareAccelerated="true">

4) And the most important, try to change your aproach, if you are using fragments you will make transactions to have only one in memory.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0

There are few ways to resolve this:

1- Replace fragments instead of Adding them. I know your fragments are getting data from server and to resolve this issue use some sort of in-memory or disk-based cache strategy in your model layer to avoid unnecessary network requests.

2- Use multiple activities and divide your application's responsibilities among all of them.

Muhammad Muzammil
  • 1,313
  • 1
  • 12
  • 28
0

use fragment replacement instead of adding them and inside of all of your fragments call setRetainInstance(true); in onCreate method before super.onCreate(savedInstanceState); to avoiding fragment to be recreated when backpressed.and inside of your fragments check did it load server data already.

zohreh
  • 1,055
  • 1
  • 9
  • 26