Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Bundle Class in Xamarin.Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 734
    Comment on it

    Hello All,

    In this we will discuss about one of the most important part of the building blocks of Xamarin.Android, and that is Bundle class.

    A Bundle class. in nothing but a mapping from values of String types to various other types that can be Parcelable. While transitioning between the states of life cycle of an Activity class, there is a built in functionality to save the states of the application or to save certain aspects of our application and that is with the help of Bundle but Bundle can also be used in many other ways as we saw earlier that while calling the OnCreate() Method, we passes a Bundle and one way to check whether your application is just started again or its just a initial run of your application, we can check that by the value of the Bundle which is initially null, when your application runs for the first time.

    So if in the below method the initialBundle is null that you can identify that your application is running for the first time, and if it is not null, that your application is woken up again and that you can also extract data from the Bundle as well.

    protected override void OnCreate(Bundle initialBundle)
    {
       base.OnCreate(initialBundle);
    }
    

    So Bundle can also be think of a simple ways to save the state data and simple peace of data of primitive types. So for the example of that we can run below code and check the value of the bundle.

    protected override void OnSaveInstanceState(Bundle Initialbundle)
    {
          Initialbundle.PutInt("Count", 1);
          base.OnSaveInstanceState(Initialbundle);
    }
    
    protected override void OnRestoreInstanceState(Bundle SavedInitialbundle)
    {
          var count = SavedInitialbundle.GetInt("Count");
    }
    

    Here we are saving may be running counter or may be an information which user has entered in Bundle so if in any case they background our application and come back, than they don't have to type those entered values again.

    Here we are using one of the given methods PutInt() by Bundle which is used to save the integer value and we can later on retrieve in with the help of GetInt() method.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: