Fabii
Fabii

Reputation: 3890

(Android) Which is better a Bundle or Application?

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;
  }

}

Upvotes: 3

Views: 300

Answers (2)

drulabs
drulabs

Reputation: 3121

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.

Upvotes: 1

Graham Smith
Graham Smith

Reputation: 25757

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");

Upvotes: 4

Related Questions