Friends In My Application , i want to use text box value in all other activity without passing any argument. how it's possible? Anyone know these give me a example, thanks in advance. by Nallendiran.S
-
Make your TextView `protected static`, Then use it from all other activities like: `MainActivity.myTextView.getText();` or `MainActivity.myTextView.setText("Some Text from Activity 2");` – Phantômaxx May 03 '14 at 12:19
-
1possible duplicate of [Android global variable](http://stackoverflow.com/questions/1944656/android-global-variable) – Joachim Isaksson May 03 '14 at 12:19
-
1Or store it as a shared preference that can be accessed throughout your application without the restrictions of a static – zgc7009 May 03 '14 at 12:32
2 Answers
There are a few different ways you can achieve what you are asking for.
1.) Extend the application class and instantiate your controller and model objects there.
public class FavoriteColorsApplication extends Application {
private static FavoriteColorsApplication application;
private FavoriteColorsService service;
public FavoriteColorsApplication getInstance() {
return application;
}
@Override
public void onCreate() {
super.onCreate();
application = this;
application.initialize();
}
private void initialize() {
service = new FavoriteColorsService();
}
public FavoriteColorsService getService() {
return service;
}
}
Then you can call the your singleton from your custom Application object at any time:
public class FavoriteColorsActivity extends Activity {
private FavoriteColorsService service = null;
private ArrayAdapter<String> adapter;
private List<String> favoriteColors = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorite_colors);
service = ((FavoriteColorsApplication) getApplication()).getService();
favoriteColors = service.findAllColors();
ListView lv = (ListView) findViewById(R.id.favoriteColorsListView);
adapter = new ArrayAdapter<String>(this, R.layout.favorite_colors_list_item,
favoriteColors);
lv.setAdapter(adapter);
}
2.) You can have your controller just create a singleton instance of itself:
public class Controller {
private static final String TAG = "Controller";
private static sController sController;
private Dao mDao;
private Controller() {
mDao = new Dao();
}
public static Controller create() {
if (sController == null) {
sController = new Controller();
}
return sController;
}
}
Then you can just call the create method from any Activity or Fragment and it will create a new controller if one doesn't already exist, otherwise it will return the preexisting controller.
3.) Finally, there is a slick framework created at Square which provides you dependency injection within Android. It is called Dagger. I won't go into how to use it here, but it is very slick if you need that sort of thing.
I hope I gave enough detail in regards to how you can do what you are hoping for.

- 133
- 2
- 8
Create it static type and than you can get it where you want.
Private TextVeiw txtvw;
Public static String myText="";
myText=txtvw.getText();
Access this variable with class name in which it defined.
MyActivity.myString

- 85
- 6
-
This idea works for simple values, but it does not work (at least reliably) for UI objects which should only be accessed by the Activity which owns them. Additionally, something marked Private is not shared anyway. – Chris Stratton May 04 '14 at 15:23
-
Yes but you can save its value in a string variable and make it static. Get value from the textView and then assign its value to static string variable. – user2323471 May 05 '14 at 05:27
-
That is not what you posted. Your posted answer is doubly unworkable. – Chris Stratton May 05 '14 at 10:12
-
Yes its my mistak. i apologize for it. But i just give the idea about it. – user2323471 May 05 '14 at 11:07