What Is a "Bundle" in an Android Application

What is a bundle in an Android application

Bundles are generally used for passing data between various Android activities. It depends on you what type of values you want to pass, but bundles can hold all types of values and pass them to the new activity.

You can use it like this:

Intent intent = new...
Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("myKey", AnyValue);
startActivity(intent);

You can get the passed values by doing:

Bundle extras = intent.getExtras(); 
String tmp = extras.getString("myKey");

You can find more info at:

  • android-using-bundle-for-sharing-variables and

  • Passing-Bundles-Around-Activities

Difference between apk (.apk) and app bundle (.aab)

App Bundles are a publishing format, whereas APK (Android application PacKage) is the packaging format which eventually will be installed on device.

App Bundles use bundletool to create a set of APK. (.apks)
This can be extracted and the base and configuration splits as well as potential dynamic feature modules can be deployed to a device.

The dependencies can look something like this:
Bundletool modules

The contents of an App Bundle look kind of like this:
Bundletool contents

More information on App Bundles is available here.

What does bundle mean in Android?

I am new to android application development and can't understand what
does bundle actually do for us. Can anyone explain it for me?

In my own words you can image it like a MAP that stores primitive datatypes and objects as couple key-value

Bundle is most often used for passing data through various Activities. Provides putType() and getType() methods for storing and retrieving data from it.

Also Bundle as parameter of onCreate() Activity's life-cycle method can be used when you want to save data when device orientation is changed (in this case activity is destroyed and created again with non null parameter as Bundle).

More about Bundle at its methods you can read reference at developer.android.com where you should start and then make some demo applications to get experience.

Demonstration examples of usage:

Passing primitive datatypes through Activities:

Intent i = new Intent(ActivityContext, TargetActivity.class);
Bundle dataMap = new Bundle();
dataMap.putString("key", "value");
dataMap.putInt("key", 1);
i.putExtras(dataMap);
startActivity(i);

Passing List of values through Activities:

Bundle dataMap = new Bundle();
ArrayList<String> s = new ArrayList<String>();
s.add("Hello");
dataMap.putStringArrayList("key", s); // also Integer and CharSequence
i.putExtras(dataMap);
startActivity(i);

Passing Serialized objects through Activities:

public class Foo implements Serializable {

private static final long serialVersionUID = 1L;

private ArrayList<FooObject> foos;

public Foo(ArrayList<FooObject> foos) {
this.foos = foos;
}

public ArrayList<FooObject> getFoos() {
return this.foos;
}
}


public class FooObject implements Serializable {

private static final long serialVersionUID = 1L;

private int id;

public FooObject(int id) {
this.id = id;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
}

Then:

Bundle dataMap = new Bundle();
ArrayList<FooObject> foos = new ArrayList<FooObject>();
foos.add(new FooObject(1));
dataMap.putSerializable("key", new Foo(foos));



Pass Parcelable objects through Activities:

There is much more code so here is article how to do it:

  • Parcel data to pass between Activities using Parcelable classes


How to retrieve data in target Activity:

There is one magic method: getIntent() that returns Intent (if there are any data also with extended data) that started Activity from there method is called.

So:

Bundle dataFromIntent = getIntent().getExtras();
if (dataFromIntent != null) {
String stringValue = dataFromIntent.getString("key");
int intValue = dataFromIntent.getInt("key");
Foo fooObject = (Foo) dataFromIntent.getSerializable("key");
// getSerializble returns Serializable so we need to cast to appropriate object.
ArrayList<String> stringArray = dataFromIntent.getStringArrayList("key");
}



Usage of Bundle as parameter of onCreate() method:

You are storing data in onSaveInstanceState() method as below:

@Override
public void onSaveInstanceState(Bundle map) {
map.putString("key", "value");
map.putInt("key", 1);
}

And restore them in onCreate() method (in this case is Bundle as parameter not null) as below:

@Override
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
String stringValue = savedInstanceState.getString("key");
int intValue = savedInstanceState.getString("key");
}
...
}

Note: You can restore data also in onRestoreInstanceState() method but it's not common (its called after onStart() method and onCreate() is called before).

Definition of Android Bundle

Bundle generally use for passing data between various Activities. It depends on you what type of values you want to pass but bundle can hold all types of values and pass to the new activity.

You can use it like ...

Intent intent = new
Intent(getApplicationContext(),SecondActivity.class);
intent.putExtra("myKey",AnyValue);
startActivity(intent);

Now you can get the passed values by...

Bundle extras = intent.getExtras(); 
String tmp = extras.getString("myKey");

you can also find more info on android-using-bundle-for-sharing-variables and Passing-Bundles-Around-Activities

Copy from Here.

Bundle ID in android

A bundle ID otherwise known as a package in Android is the unique identifier for all Android apps. It needs to be unique as when you upload it to Google Play it identifies and publishes your app using the package name as the unique app identification.

Really it is the only thing which is necessary to identify your app, and generally it has 3 parts:

com.example.testapp

Where example is generally the company/publishers name, and testapp is the appname.

You will not be able to upload an APK to the store which has the same package as another app already in the store.

Should you ever need to change the package name in Eclipse, do the following:

Right click project > Android Tools > Rename Application Package...

Use of Build Bundle Option in Android studio 3.2

What is App Bundle?

An Android App Bundle is a new upload format that includes all your app’s compiled code and resources, but defers APK generation and signing to Google Play.

Google Play’s new app serving model, called Dynamic Delivery, then uses your app bundle to generate and serve optimized APKs for each user’s device configuration, so they download only the code and resources they need to run your app. You no longer have to build, sign, and manage multiple APKs to support different devices, and users get smaller, more optimized downloads

Uses of App Bundle

  1. Dynamic Delivery :

Dynamic Delivery is Google Play's new app serving model, and it uses your app bundle to generate and serve optimized APKs for each user's device configuration, so they download only the code and resources they need to run your app. For example, user won't need other languages strings if he have set English as his default language.


  1. Dynamic feature modules :

Dynamic feature modules allow you to separate certain features and resources from the base module of your app and include them in your app bundle. Through Dynamic Delivery, users can later download and install those components on demand after they've already installed the base APK of your app. You can use Play Core Library you can download these modules when requested .

Sample Image

On the left: a simple app that includes a base APK (B) and some configuration APKs (C). On the right: a more complex app that includes two dynamic feature APKs (D) and corresponding configuration APKs (C) for download on demand.


  1. No need for having multiple APKs :

The dynamic delivery takes care of the split apk . A fundamental component of Dynamic Delivery is the split APK mechanism available on Android 5.0 (API level 21) and higher. With split APKs Google Play can break up a large app into smaller, discrete packages that are installed on a user's device as required.

optimizing the APK content are based on the following:

  • Locale
  • Screen density
  • CPU architecture

More info can be found here


  1. Smaller Apk size :

As app are broke in smaller parts , that means when user downloads you app it will be of smaller size for him . On average, apps published with app bundles are 20% smaller in size.

Testing App Bundles

After you build your Android App Bundle, you should test how Google Play uses it to generate APKs and how those APKs behave when deployed to a device. There are two ways you should consider testing your app bundle:

  • Locally using the bundletool command line tool
  • Through Google Play by uploading your bundle to the Play Console and using the new internal test track.

Sources

  • Android official documentation
  • Codelabs
  • Medium blog post

(Android) Which is better a Bundle or Application?

This is similar to this question What is a "bundle" in an Android application, which contains a comprehensive answer with example.

My answer would be that you would use a bundle as this is what they were designed for and are easy enough to use. The bundle supports a String without any extra work being done so I would argue it makes it ideal.

Adding to intent

intent.putExtra("myKey",AnyValue);  

Retrieving:

Bundle extras = intent.getExtras(); 
String tmp = extras.getString("myKey");

Android: what is the difference between Bundle Vs java.util collections like HashMap

One difference I can see easily is that a Bundle allows you to put int, boolean, etc., into it while a HashMap seems to require you to convert them to/from objects.

A more important difference is that with a Bundle, every object it contains is parcelable. This allows Bundles to be used as service parameters and to be attached to intents / passed into activities.

Creating a bundle to publish an Android app

When you build signed bundle like this

Build -> Generate Signed Bundle / APK...

Android Studio must notify you about successful build with this notification in bottom-right corner of screen:
Sample Image

Once you click on locate word in this notification, you will be redirected to the real directory where bundle created is.

If you can't see this notification, maybe your build was failed? Are you sure build is success? Are you sure you build a release version, not debug?



Related Topics



Leave a reply



Submit