Passing Intent data from Activity to Class - android

I want to pass string from my activity class to my class that is not extended to activity. I know this is possible but how to do that? I am new to android development so please help

In android you can send data through Intent or Intent followed by Bundle like:
Intent i = new Intent(current_activity.this, linked_class.class);
i.putextra("Key", value);
And get the value(suppose string value) in another class like:
String value = getIntent.getExtra("String key which you used when send value");
Option 2:
class yourClass {
public static String _utfValue = "";
void sendValue(){
_utfValue = "some value";
}
}
And fetch this value in your java class like:
String value = A._utfValue ;
Option 3: You can use SharedPreference to save the value and get it from other class.
Option 4: You can use a static method with some return value and fetch the method in your java class through class name.
All the options are rough here. So just check this ,hope one of these will help you .

You create static variable and access anotherclass
like
Static String mydemo=null;
mydemo="sagar";
then access directly in another class

Related

How to store global variables using setter and getter in Android?

i am new to android development and i was trying to use a global variable across two activities. So how do i do the same using setter and getter? or is there a better way?
Please do help me!
Thanks in advance!
Sidharth
For global variable :
Use can use SharedPreference, which will save the value till you uninstall the application and can be accessed from anywhere within application using context.
Extend Application class, and declare the global variable inside it and add getter and setter methods.
And in your activity :
YourApplication yourApplication = (YourApplication) getApplicationContext();
yourApplication.setGlobalValue(10);
yourApplication.getGlobalValue();
Create class :
class YourApplication extends Application {
private Integer globalValue;
public Integer getGlobalValue() {
return globalValue;
}
public void setGlobalValue(Integer value) {
globalValue = value;
}
}
The easiest way to do this would be to pass the variable to the second activity in the intent you're using to start the activity:
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("variableKEY", variable);
startActivity(intent)
Access that intent on next activity
String s = getIntent().getStringExtra("variableKEY");
The docs for Intents has more information (look at the section titled "Extras").
From here

Is it necessary to write URL of webservice in each page

Is it necessary to write URL of web-service in each page in android application or any other way where i can save URL once for the whole application.
I have a android application where i am calling web-service . I am new to android so i don't have idea of saving the URL globally. How i can save the URL once in the whole application.
private final String URL = "http://192.1.1.1/Service1.asmx";
Instead of writing in every .java file what else i can do.
Create a class in java and declare a String field like the following in that class,
public class MyConstants {
public static final String Url = "http://192.1.1.1/Service1.asmx";
}
then you can access it globally from any class like the following
String url = MyConstants.Url;
As Url is a static final field of MyConstants class, so you can access it just with the name of the class(without creating the object of the class with new Operator). i.e MyConstants in this case.
For Learning more about static and to see how this thing works, Please refer to this link
final means that the value cannot be changed after initialization, that's what makes it a constant. static means that instead of having space allocated for the field in each object, only one instance is created for the class.
So, static final means only one instance of the variable no matter how many objects are created and the value of that variable can never change.
If you don't want to create a class you can just put it on res/values/strings
<string name="URLWebService">http://192.1.1.1/Service1.asmx</string>
Then in each class you need it do this :
getResources().getString(R.string.URLWebService);
And you can do it directly or just put a
public static final String URL = getResources().getString(R.string.URLWebService);
You can use what you want all are going to work fine.
You can also use the string file in res/values.
You can you like following:
<string name="ws_url">http://192.1.1.1/Service1.asmx</string>
and use it so: String wsUrl = getString(R.string.ws_url)
create a global constant class and save constants there :
public class Constants {
public static final String Url = "http://192.1.1.1/Service1.asmx";
}
And access it anywhere in application by just calling
Constants.Url
Create common class YourGlobalClass:
public class YourGlobalClass{
public static final String URL = "192.1.1.1/Service1.asmx";
}
And wherever you want just call the class name. your variable name
ex. YourGlobalClass.URL;

passing variable status between classes

I am making a boolean variable value in one class and accessing the status of that variable in another class , on the basis of the boolean variable status my list view shows items,
So my question is
how to create a global boolean varible
how to pass it to another class
how the second class check it
1. Variable in class A
public class A{
public String aClassVar="hello";
}
Using it in Class B
A obj=new A();
String inBClass=obj.aClassVar;
2. To pass data from one Activity to Another Activity you can use Intent remember your Class should extends Activity only than you will be able to pass data by using Intent
Example
Send Data From First Activity using :
Intent i = new Intent(this, SecondClassName.class);
i.putExtra("key", "Value");// key is used to get value in Second Activiyt
startActivity(i);
Receive Data on Second Activity using:
Intent intent = getIntent();
String temp = intent.getStringExtra("key");// usr getStringExtra() If your extra data is represented as strings:
And you Must set Activity Name inside AndroidManifest.xml
like:
<activity android:name="yourPackageName.SecondClassName" />
Let me suggest 3 options.
Do you want to pass a boolean variable between Android Activities? If so, You may want to use a Bundle. Yes, those little things given to Activities on onCreate(). You can pass variables of your own into these, in your case a boolean, with putBoolean() and getBoolean()
Would you prefer using Android's SharedPref? It's an interface for sharing small preferences, like boolean flags, between parts of your app and storing it for later.
Or, you could just implement a singleton class that has the boolean variable and other variables you need to store and check by different classes within your app.
If you just want to access the value of an object or variable in another class, make it Static and then when you need its value, do like
public class tempClass {
public void tempMethod() {
boolean status = myClass.myVariable ; // where myVariable is static boolean varaible of myClass
}
}
But make sure to access the variable after some value is stored in it.
If you want to send the value to another activity then send the value by using intent.
Eg.
Bundle myBund = new Bundle();
myBund.putBoolean(myBool);
Intent intent = new Intent(myClass.this, tempClass.class);
intent.putExtras("myKey",myBund);
startActivity(myBund);

Passing a simple variable value for use in another class in Android

What is the correct way to pass a simple variable which has its value set to use in another class? I have a counter which is incremented with a button press and when counter = 3 a method is called which loads another activity which loads another screen with a string which states the button was pressed. Here is where I want to include the number 3 passed from the counter variable in the previous class, but I am not sure the correct way to do this.
I tried creating an instance of the class and connecting to the variable that way but the value is never passed and it is always 0.
Try following code,
public class First
{
private static int myVarible = 0;
private void myMethod()
{
myVariable = 5; // Assigning a value;
}
public static int getVariable()
{
return myVariable;
}
}
public class Second
{
int i = First.getVariable(); // Accessing in Another class
}
You may pass value via Intent - resource bundle.
Pass value from current activity,
Intent intent=new Intent(this,ResultActivity.class);
intent.putExtra("no",10);
startActivity(intent);
and in ResultActivity (onCreate),
Bundle bundle=getIntent().getExtras();
int value=bundle.getInt("no");
In case of non-activity classes, you may define a public method.
For instance,
public Integer getValue() //returns a value
{
return 10;
}
Or
public void setValue(Integer value)
{
...
}
one way to do it would be when you build your new intent to do :
Intent itn = new Intent(...,...);
itn.putExtra(KEY,VALUE);
startctivity(itn);
And on the other class do something like :
this.getIntent.getStringExtra(KEY);
or
this.getIntent.getWHATEVERYOURVARIABLEWASExtra(KEY);
Hope it helps you, and it's the better way to pass arguments between 2 activities :)
You can also do it through define variable as "static" in one activity and in second activity use that variable with using <<classname>>.<<variableb>>
ex:
Activity1:
Static var = 5;
Activity2:
System.out.println(Activity1.var);
You can pass that value using the intent
Intent new_intent=new Intent(this,SecondActivity.class);
intent.putExtra("counter",10); //here the value is integer so you use the new_intent.putExtra(String name,int value)
startActivity(new_intent);
After that in the second activity class you can get that that values using Bundle
Bundle bundle = getIntent().getExtras();
int count = bundle.getInt("counter");
I think this may help you.

android pass ArrayList<self_Obj> by Intent

I had create a class named ChannelObj that contains values like this
public class ChannelObj {
public String enable;
public String id;
public String name;
public String ptz;
public ChannelObj(Node n){
this.enable = n.getAttributes().getNamedItem("enabled").getNodeValue();
this.id = n.getAttributes().getNamedItem("id").getNodeValue();
this.name = n.getAttributes().getNamedItem("name").getNodeValue();
this.ptz = n.getAttributes().getNamedItem("ptz").getNodeValue();
}
}
and this Class can create Obj that contains what data I need;
after that,I have an ArrayList named allChannel contains all ChannelObj i have
like this
for(int i = 0;i<num_of_channel;i++)
{
allChannel.add(new ChannelObj(n1.item(i)));
}
i've checked the data in allChannel is correct
but i want pass this ArrayList to next Activity
i've tried ways like
Intent i = new Intent(this,ChannelListActivity.class);
Bundle b = new Bundle();
b.putParcelableArrayListExtra("dd", ArrayList<ChannelObj> allChannels);
i.putExtra(String name,b);
startActivity(i);
but didn't work and still wrong
what i suppose to do?
thanks for your help!
An alternative to the answer given by Benoir is to have your ChannelObj class implement the Serializable interface. You're only using simple data types, so all the (de)serializing will be automa(g)(t)ically done underwater.
If your class implements Serializable, then you can add it to a Bundle as follows:
bundle.putSerializable("CHANNELOBJ_LIST", mChannelObjList);
Note that you may need to cast to an ArrayList<ChannelObj> (or some other concrete implementation of List<T>) as the List<T> interface does not implement Serializable.
Retrieving the list of objects in the next activity is similarly easy:
List<ChannelObj> mChannelObjList = (ArrayList<ChannelObj>) bundle.getSerializable("CHANNELOBJ_LIST");
Your clas must implement Parcelable check it out here: http://developer.android.com/reference/android/os/Parcelable.html

Categories

Resources