i'm new in developing android in xamarin i just want to ask in how to pass data through other activity using intent ? This thing work (https://developer.xamarin.com/recipes/android/fundamentals/activity/pass_data_between_activity/) but i want to collect first all the data in 2 activity before they show the summary in my 3rd activity.(by the way i'm creating a registration app thanks for the future answers :) )
On the first activity you create the second activity by an intent and use the method PutExtra to pass the data you want with a relevant key name that you will need after starting the new activity to retrieve the data.
var secondActivity = new Intent (this, typeof(SecondActivity));
secondActivity.PutExtra ("Data", "Sample Data");
StartActivity(secondActivity);
On second activity OnCreate retrieve the data using the key name it was stored with and the correct method relevant to the data type passed. In this example as is a string by calling Intent.GetStringExtra.
string text = Intent.GetStringExtra ("Data") ?? "Data not available";
You can repeat 1 & 2 for the summary activity.
you can pass whole object and desterilize that in another activity like this
//To pass:
intent.putExtra("yourKey", item);
// To retrieve object in second Activity
getIntent().getSerializableExtra("yourKey");
For only single value you can use
//Method 1
string code = Intent.GetStringExtra("id") ?? string.Empty;
string name = Intent.GetStringExtra("Name") ?? string.Empty;
//OR
//Method 2
string Id = Intent.GetStringExtra("id") ?? string.Empty;
Item item = new Item();
item = itemRepo.Find(Convert.ToInt32(id));
Related
So I tried the intent method but I always get a red underline under the "putExtra"
here is the code I tried:
next.setOnClickListener {
var i=Intent(this,Screen6::class.java)
i.putExtra("ID", id)
}
How can I do that?
EDIT:
the id needed the function to.toString() but now I need to get it from the other activity how is that possible
You can simply use the intents to send data and parameters from one activity to another activity in kotlin
val intent = Intent(this#OneActivity,TwoActivity::class.java);
intent.putExtra("username", userName)
startActivity(intent);
Where "username" will be key and userName will be actual data you will pass.
I'm trying to transfer ArrayList between activities, but nothing I've tried works as well.
This was my best shot, but this didn't work either.
I'm calling the external action here:
getComics getComicInfo = new getComics(charName, pageNum);
getComicInfo.execute();
getIntentData()
and here i'm trying to put data, but the problem is due the fact that this is an external action, so I can't shift through activits.
if(counter == comicInfoObject.length()){
Log.v("check arr length =>" , Integer.toString(comicList.size()));
Intent i = new Intent();
i.putExtra("comicArrayList", (ArrayList<Comics>)comicList);
}
and here i'm tring to retrive the data, but it doesn't get inside the "if"
public void getIntentData(){
Intent i = getIntent();
if(i != null && i.hasExtra("comicArrayList")){
comicList2 = i.getParcelableExtra("comicArrayList");
int size = comicList2.size();
}
}
first code is where I call an external class that using api and in the bottom line creates the arrayList
second code is inside the external class, where I'm trying to pass the arrayList with putExtra
third code is where i'm tring to retrive the data after getIntentData().
Pack at the sender Activity
intent.putParcelableArrayListExtra(<KEY>, ArrayList<Comics extends Parcelable> list);
startActivity(intent);
Extract at the receiver Activity
getIntent().getParcelableArrayListExtra(<KEY>);
You need make a Comics class that implements Parcelable, then use put ParcelableArrayListExtrato pass arraylist.
Here is a sample link for Pass data between activities implements Parcelable
However be careful if your array list too big, you could get intent exception, for this case you could think about a static reference to store your array list.
I have a problem with with sharing data between two different activities. I have data like :
int number
String name
int number_2
int time
int total
I'm trying to make something like order list with this set of data . So it will take one set of data , then back to previous activity , move forward and again add data to it .
I have an idea of making it in array of object - but data inside was cleared after changing activity.
How can I make it ?
I don't know if and how to add Array of object to SharedPreferences , and get value of one element from there.
You should have a look at the documentation of the Intent(s) if you want to do that on the fly associating a key to the value(s) that you want to pass to your second activity.
Anyway, you can think any(sharedpref, database,...) way to pass your parameters but for those kind of things it's a convention and a good practice to follow that.
Don't used share preferences for this...Use the singleton pattern, extend Application, or just make a class with static variables and update them...
You can use .putExtra but since you are communicating with more than one activity the above suggestions are probably the best.
public class ShareData {
private String s;
private int s;
private static ShareData shareData = new ShareData();
private ShareData(){}
public static ShareData getInstance(){ return shareData}
//create getters and setters;
}
Why not to use Intents
Intent intent = new Intent(FirstActivity.this, (destination activity)SecondActivity.class);
intent.putExtra("some_key", value);
intent.putExtra("some_other_key", "a value");
startActivity(intent);
in the second activity
Bundle bundle = getIntent().getExtras();
int value = bundle.getInt("some_key");
String value2 = bundle.getString("some_other_key");
EDIT if you want to read more about adding array to shared preferences check this
Is it possible to add an array or object to SharedPreferences on Android
also this
http://www.sherif.mobi/2012/05/string-arrays-and-object-arrays-in.html
I'm a noob to android development and I am attempting to pass a variable between two classes. According to my debugger, the value is being set in the first class, but for whatever reason it is returning null in the second class. I initially tried passing the value by making the variable public and static and then I attempted placing the value in the Extras. Both methods returned null in the second Activity. I have clean the project with no success. It seems as if it just won't won't allow the value of the string to be reset. Any help resolving this is greatly appreciated.
My Code
CLASS A
case R.id.profile:
selection = (Integer)v.getTag();
Log.e("Selection", String.valueOf(selection));
if(LandingActivity.user_isClient){
adapter_email = (listData.get(selection)).get("Email"); //<--debugger showing value as set here
Log.e("Adapter Email", adapter_email);
Intent myIntent = new Intent("com.tpssquared.kuttime.PROFILE_BARBER_VIEW");
myIntent.putExtra("ADAPTER_EMAIL", adapter_email); //<--debugger showing value as set here too
c.startActivity(myIntent);
}else{
adapter_email = (listData.get(selection)).get("Client_Email"); //<--debugger showing value as set here
Log.e("Adapter Email", adapter_email);
Intent myIntent = new Intent("com.tpssquared.kuttime.PROFILE_CLIENT_VIEW");
myIntent.putExtra("ADAPTER_EMAIL", adapter_email); //<--debugger showing value as set here too
c.startActivity(myIntent);
}
break;
CLASS B
barber_email_string ="";//Deubugger showing value as null and not "". WHY?
try{
if(barber_email_string.equals(null)||barber_email_string.equals("")){
barber_email_string = getIntent().getStringExtra("ADAPTER_EMAIL");
if(barber_email_string.equals(null)||barber_email_string.equals("")){
barber_email_string = Uri.encode(MyAppointments_ListAdapter.adapter_email);
Log.e("BPV email",barber_email_string);
}
}
Log.e("BPV email",barber_email_string);
}catch(Exception e){
e.printStackTrace();
Log.e("On Create", e.toString());
}
You aren't passing a Bundle to Activity B, you're passing a String extra. So when you call getExtras(), there's nothing there. You can simply call getStringExtra() on the Intent itself to get your string.
barber_email_string = getIntent().getStringExtra("ADAPTER_EMAIL");
Alternatively, you could create a new Bundle, package the string into that, and pass it with putExtras(myBundle), but that's overkill for a single string.
I would like to be able to transfer data from one activity to another activity. How can this be done?
Through the below code we can send the values between activities
use the below code in parent activity
Intent myintent=new Intent(Info.this, GraphDiag.class).putExtra("<StringName>", value);
startActivity(myintent);
use the below code in child activity
String s= getIntent().getStringExtra(<StringName>);
There are couple of ways by which you can access variables or object in other classes or Activity.
A. Database
B. shared preferences.
C. Object serialization.
D. A class which can hold common data can be named as Common Utilities it depends on you.
E. Passing data through Intents and Parcelable Interface.
It depend upon your project needs.
A. Database
SQLite is an Open Source Database which is embedded into Android. SQLite supports standard relational database features like SQL syntax, transactions and prepared statements.
Tutorials -- http://www.vogella.com/articles/AndroidSQLite/article.html
B. Shared Preferences
Suppose you want to store username. So there will be now two thing a Key Username, Value Value.
How to store
// Create object of SharedPreferences.
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
//now get Editor
SharedPreferences.Editor editor = sharedPref.edit();
//put your value
editor.putString("userName", "stackoverlow");
//commits your edits
editor.commit();
Using putString(),putBoolean(),putInt(),putFloat(),putLong() you can save your desired dtatype.
How to fetch
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String userName = sharedPref.getString("userName", "Not Available");
http://developer.android.com/reference/android/content/SharedPreferences.html
C. Object Serialization
Object serlization is used if we want to save an object state to send it over network or you can use it for your purpose also.
Use java beans and store in it as one of his fields and use getters and setter for that
JavaBeans are Java classes that have properties. Think of
properties as private instance variables. Since they're private, the only way
they can be accessed from outside of their class is through methods in the class. The
methods that change a property's value are called setter methods, and the methods
that retrieve a property's value are called getter methods.
public class VariableStorage implements Serializable {
private String inString ;
public String getInString() {
return inString;
}
public void setInString(String inString) {
this.inString = inString;
}
}
Set the variable in you mail method by using
VariableStorage variableStorage = new VariableStorage();
variableStorage.setInString(inString);
Then use object Serialzation to serialize this object and in your other class deserialize this object.
In serialization an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.
After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.
If you want tutorial for this refer this link
http://javawithswaranga.blogspot.in/2011/08/serialization-in-java.html
Get variable in other classes
D. CommonUtilities
You can make a class by your self which can contain common data which you frequently need in your project.
Sample
public class CommonUtilities {
public static String className = "CommonUtilities";
}
E. Passing Data through Intents
Please refer this tutorial for this option of passing data.
http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/
When you passing data from one activity to another activity perform like this
In Parent activity:
startActivity(new Intent(presentActivity.this, NextActivity.class).putExtra("KEY_StringName",ValueData));
or like shown below in Parent activity
Intent intent = new Intent(presentActivity.this,NextActivity.class);
intent.putExtra("KEY_StringName", name);
intent.putExtra("KEY_StringName1", name1);
startActivity(intent);
In child Activity perform as shown below
TextView tv = ((TextView)findViewById(R.id.textViewID))
tv.setText(getIntent().getStringExtra("KEY_StringName"));
or do like shown below in child Activity
TextView tv = ((TextView)findViewById(R.id.textViewID));
TextView tv1 = ((TextView)findViewById(R.id.textViewID1))
/* Get values from Intent */
Intent intent = getIntent();
String name = intent.getStringExtra("KEY_StringName");
String name1 = intent.getStringExtra("KEY_StringName1");
tv.setText(name);
tv.setText(name1);
Passing data from one activity to other in android
Intent intent = new Intent(context, YourActivityClass.class);
intent.putExtra(KEY, <your value here>);
startActivity(intent);
Retrieving bundle data from android activity
Intent intent = getIntent();
if (intent!=null) {
String stringData= intent.getStringExtra(KEY);
int numberData = intent.getIntExtra(KEY, defaultValue);
boolean booleanData = intent.getBooleanExtra(KEY, defaultValue);
char charData = intent.getCharExtra(KEY, defaultValue); }
Hopefully you will find the answer from here Send Data to Another Activity - Simple Android Login
You just have to send extras while calling your intent
like this:
Intent intent = new Intent(getApplicationContext(), SecondActivity.class); intent.putExtra("Variable Name","Value you want to pass"); startActivity(intent);
Now on the OnCreate method of your SecondActivity you can fetch the extras like this
If the value u sent was in "long"
long value = getIntent().getLongExtra("Variable Name which you sent as an extra", defaultValue(you can give it anything));
If the value u sent was a "String"
String value = getIntent().getStringExtra("Variable Name which you sent as an extra");
If the value u sent was a "Boolean"
Boolean value = getIntent().getStringExtra("Variable Name which you sent as an extra",defaultValue);
Your Purpose
Suppose You want to Go From Activity A to Activity B.
So We Use an Intent to switch activity
the typical code Looks Like this -
In Activity A [A.class]
//// Create a New Intent object
Intent i = new Intent(getApplicationContext(), B.class);
/// add what you want to pass from activity A to Activity B
i.putExtra("key", "value");
/// start the intent
startActivity(i);
In Activity B [B.class]
And to Get the Data From the Child Activity
Intent i = getIntent();
if (i!=null) {
String stringData= i.getStringExtra("key");
}
This works best:
Through the below code we can send the values between activities
use the below code in parent activity(PARENT CLASS/ VALUE SENDING CLASS)
Intent myintent=new Intent(<PARENTCLASSNAMEHERE>.this,<TARGETCLASSNAMEHERE>.class).putExtra("<StringName>", value);
startActivity(myintent);
use the below code in child activity(TARGET CLASS/ACTIVITY)
String s= getIntent().getStringExtra(<StringName>);
Please see here that "StringName" is the name that the destination/child activity catches while "value" is the variable name, same as in parent/target/sending class.