6

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);
    }
}
Aruva
  • 95
  • 2
  • 8
  • The question is, where is the class defined and in which context does the instantiation live from which you want to "pass" the "variable". Also, variables cannot be _passed_, they could be _shared_; on the other hand, _values_ and _references_ could be passed. So do you have any specific requirements regarding this. – class stacker Feb 12 '13 at 09:20
  • do you have an instance of that class? if yes use a getter to retrieve the string and use it inside the setText. eg t1.setText(around.getString()); – Tobrun Feb 12 '13 at 09:20
  • Is the class an asynctask? or is it just called? If you just call it, you can return the value like in any normal methodcall. Just make a getter. – Gjordis Feb 12 '13 at 09:21

6 Answers6

2

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);
    }
Yogesh Tatwal
  • 2,722
  • 20
  • 43
2

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);
 }
 }
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22
1

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..

0

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.

Community
  • 1
  • 1
Mark
  • 5,466
  • 3
  • 23
  • 24
0

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.

yigit
  • 37,683
  • 13
  • 72
  • 58
0

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