Reputation: 2273
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
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;
}
}
Set your drawable
MyDrawable.setDrawable(yourDrawable);
//or Drawable d = MyDrawable.setDrawable();
//d=yourDrawable;
Get it in your other activity
yourDrawable = MyDrawable.getDrawable();
Upvotes: 1
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