Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Android) Which is better a Bundle or Application?

Tags:

android

bundle

I am working on an android application that pulls data from a Database. I want to pass data between activities (A single String). I initially implemented the data passing using the Bundle feature. However, I came across the Application class which allows a variable to be accessed from any activity.

Which would you recommend using for moving data between activities?

public class MyVideo extends Application {

  private String url ="NULL";

  public String getUrl(){
    return url;
  }
  public void setUrl(String newurl){
    url = newurl;
  }

}

like image 916
Fabii Avatar asked Dec 03 '25 10:12

Fabii


2 Answers

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");
like image 180
Graham Smith Avatar answered Dec 06 '25 00:12

Graham Smith


Application class will behave as a singleton class in your context. You can pass data between activities using singleton class itself. No need to use Application class if all you want is to pass data between activities.

Bundle is preferable for passing data b/w activities.

like image 32
drulabs Avatar answered Dec 05 '25 23:12

drulabs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!