mr nooby noob
mr nooby noob

Reputation: 2273

How do I pass in or access a value from one intent to another?

How can I access a method or variable from one intent to another?

I want to do something with a drawable from one intent but I dont know how to access it. Creating an object isn't going to work for my case.

Upvotes: 0

Views: 124

Answers (2)

user4624062
user4624062

Reputation:

You need to use a Singleton.
Here is an example

    public class MyDrawable 
    {

        //singleton
        private static MyDrawable mydrawable=null;

        Drawable drawable=null;

        //only setDrawable creates objects
        private MyDrawable(){}

        private MyDrawable(Drawable drawable)
        {
            this.drawable = drawable;
        }

       public static Drawable getDrawable()
       {
           if(mydrawable==null)
               return null;
           return mydrawable.drawable;
       }


       public static Drawable setDrawable()
       {
           mydrawable =null;
           mydrawable = new MyDrawable();
           return mydrawable.drawable;
       }


       public static Drawable setDrawable(Drawable drawable)
       {
           mydrawable =null;
           mydrawable = new MyDrawable(drawable);
           return mydrawable.drawable;
       }

    }
  1. Set your drawable

       MyDrawable.setDrawable(yourDrawable);
       //or Drawable d = MyDrawable.setDrawable();
       //d=yourDrawable;
    
  2. Get it in your other activity

       yourDrawable = MyDrawable.getDrawable();
    

Upvotes: 1

DVN
DVN

Reputation: 114

In your current Activity, create a new Intent:

Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);

Then in the new Activity, retrieve those values:

Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}

Use this technique to pass variables from one Activity to the other.

Upvotes: 0

Related Questions