Aruva
Aruva

Reputation: 95

Pass variable between non-activity class to android activity class

I need to pass a variable from (around)non-activity class to an nameget(android activity)class . Display the passed variable value in text view.. Please tel me what needs to do from this example. How to pass it to android activity

public class around(non-activity class)
{
    String name = "arjun";
    //how to pass this name value to an below activity
    nameget nam = new nameget();
    String new = nam.get(name);
}

public class nameget extends Activity(android activity class)
{
   public String get(String name)
   {
      String got = name;
      TextView t1 = (TextView)findViewById(R.id.textView1);
      t1.setText(name);
    }
}

Upvotes: 6

Views: 8515

Answers (6)

user3402040
user3402040

Reputation:

Is more easy if you save a variable of type "Class"

Example : (have fun)

Class<?> MyClassSave;

MyClassSave = MainActivity.class;

And you can get all values from .class

you can make public/private (static), or pass for bundle / serializable / Obj

Upvotes: 0

yigit
yigit

Reputation: 38253

the best approach for such problems is having an event drivent architecture. you can use any event library, I would suggest eventBus .

(in scope of eventbus) create a sticky event called NameChangedEvent with a name property

your activity will listen to it while active (on resume - on pause). when name is changed, you'll dispatch this event e.g.

EventBus.getDefault().post(new NameChangedEvent(name));

your activity will catch this and update the view. it can also read the latest version of this event onCreate method.

this is of course an overkill if you are trying to do this just for 1 field. but many apps need to handle data changes and update their UI and having events is the best way (imho) to do it. this helps you loosely couple your app logic and UI.

Upvotes: 0

Mark
Mark

Reputation: 5566

Quite a strange question because your design is not clear here. You pass parameters to activity via intent bundle. You can only pass them when starting activity. Activity can be only started on some Context object (in 99% cases other Activity).

You can start activity from some non-activity class as long as you have access to context there. Activity that you will start needs to know who is it's parent in case of back button press.

When you want to pass data to activity while starting it see: How do I pass data between Activities in Android application?

When it's not this case - please describe your case what is your non-activity class and how is it related to activity that you want pass data to.

Upvotes: 0

satish kumar rajput
satish kumar rajput

Reputation: 261

You can simply generate getter and setter for your variable in non-activity class like..

public String getName(){
  return name;
}

public void setName(String name){
   this.name = name;
}

now from anywhere you can get/set value for name as..

Arround ar = new Arround()
ar.setName("Aruva"); //To set name
ar.getName();  // To get 

In your Activity do like..

 t1.setText(ar.getName().toString());

And your non-activity class can be created anywhere either in same package or in another package in src folder..

Upvotes: 1

MuraliGanesan
MuraliGanesan

Reputation: 3261

Try this, declare your non activity class in you activity class.

 public class around(non-activity class)
{
Public static String name;
 name = "arjun";
//how to pass this name string to an below activity
}

 public class nameget extends Activity(android activity class)
  {
  around ar = new around();
  //declare non activity class here

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
TextView t1 = (TextView)findViewById(R.id.textView1);
t1.setText(ar.name);
 }
 }

Upvotes: 2

Yogesh Tatwal
Yogesh Tatwal

Reputation: 2751

Try this

  public class around(non-activity class)
    {
     public static  String name = "arjun";
        //how to pass this name string to an below activity
    }

    public class nameget extends Activity(android activity class)
    {
        TextView t1 = (TextView)findViewById(R.id.textView1);
//your class name around
        t1.setText(around.name);
    }

Upvotes: 2

Related Questions