Related
I have the first intent, it starts the second intent. In the second intent, I get the values, and pass the value to the first content and close the second content. How can I do it?
You can directly pass parameters to the intent when you create it. If you need to pass objects you need to implement Parcelable interface on the object you pass:
Intent i = new Intent(MyActivity.this, SecondActivity.class);
MyData j = new MyData();
i.putExtra("MyParameter", "Something");
i.putExtra("MyData", j); //only works if MyData implements Parcelable
startActivity(i);
In the second activity you can read your data:
Intent i = getIntent();
Bundle extras = i.getExtras();
if(extras.containsKey("MyParameter")) {
String something = i.getStringExtra("MyParameter");
}
if(extras.containsKey("MyData")) {
MyData otherthing = i.getParcelableExtra("MyData");
}
Hope this helps
try this
Passing the parameter to using intent i am passing the message like this
Intent intent= new Intent(mContext,SuccessActivity.class);
intent.putExtra("message",mContext.getString(R.string.success_sign_msg));
Get value using intent
Intent intent=getIntent();
success_msg_txt.setText(intent.getStringExtra("message"));
try this it helps you
Sending array to another intent:
Intent intent = new Intent(Gallery.this, Upload.class);
intent.putExtra("image", selectImages);
startActivity(intent);
What is the most simple way to get the data and pass to listview?
This is an array, so i need to run a loop?
Thanks.
I am using this code to send data from one activity :
Intent intent;
intent = new Intent(getApplicationContext(), VerifyOTP.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("user_name", user_name);// for single data
startActivity(intent);
For an Array you can use :
intent.putStringArrayListExtra("Array", arrayNmae);
And use this in another activity's onCreate
Intent intent = getIntent();
if (intent!=null) {
user_name = intent.getStringExtra("user_name");
}
For array list use this :
ArrayList<String> array = new ArrayList<String>(); // initialize an array
Intent intent = getIntent();
if (intent!=null) {
array = intent.getStringArrayListExtra("Array");
}
Hope this will work for you.
You can define static
public static someArray
//and access
Your_Activity.someArray
To send array from one activity to another,
intent.putStringArrayListExtra("images", selectedImages);
To receive data,
Bundle bundle = getIntent().getExtras();
ArrayList<String> selectedImages = bundle.getStringArrayList("images");
How can I send data from one activity (intent) to another?
I use this code to send data:
Intent i=new Intent(context,SendMessage.class);
i.putExtra("id", user.getUserAccountId()+"");
i.putExtra("name", user.getUserFullName());
context.startActivity(i);
First, get the intent which has started your activity using the getIntent() method:
Intent intent = getIntent();
If your extra data is represented as strings, then you can use intent.getStringExtra(String name) method. In your case:
String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");
In the receiving activity
Bundle extras = getIntent().getExtras();
String userName;
if (extras != null) {
userName = extras.getString("name");
// and get whatever type user account id is
}
// How to send value using intent from one class to another class
// class A(which will send data)
Intent theIntent = new Intent(this, B.class);
theIntent.putExtra("name", john);
startActivity(theIntent);
// How to get these values in another class
// Class B
Intent i= getIntent();
i.getStringExtra("name");
// if you log here i than you will get the value of i i.e. john
Add-up
Set Data
String value = "Hello World!";
Intent intent = new Intent(getApplicationContext(), NewActivity.class);
intent.putExtra("sample_name", value);
startActivity(intent);
Get Data
String value;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
value = bundle.getString("sample_name");
}
Instead of initializing another new Intent to receive the data, just do this:
String id = getIntent().getStringExtra("id");
Put data by intent:
Intent intent = new Intent(mContext, HomeWorkReportActivity.class);
intent.putExtra("subjectName", "Maths");
intent.putExtra("instituteId", 22);
mContext.startActivity(intent);
Get data by intent:
String subName = getIntent().getStringExtra("subjectName");
int insId = getIntent().getIntExtra("instituteId", 0);
If we use an integer value for the intent, we must set the second parameter to 0 in getIntent().getIntExtra("instituteId", 0). Otherwise, we do not use 0, and Android gives me an error.
If used in a FragmentActivity, try this:
The first page extends FragmentActivity
Intent Tabdetail = new Intent(getApplicationContext(), ReceivePage.class);
Tabdetail.putExtra("Marker", marker.getTitle().toString());
startActivity(Tabdetail);
In the fragment, you just need to call getActivity() first,
The second page extends Fragment:
String receive = getActivity().getIntent().getExtras().getString("name");
If you are trying to get extra data in fragments then you can try using:
Place data using:
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER);
Get data using:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getArguments().getInt(ARG_SECTION_NUMBER);
getArguments().getString(ARG_SECTION_STRING);
getArguments().getBoolean(ARG_SECTION_BOOL);
getArguments().getChar(ARG_SECTION_CHAR);
getArguments().getByte(ARG_SECTION_DATA);
}
Kotlin
First Activity
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("key", "value")
startActivity(intent)
Second Activity
val value = getIntent().getStringExtra("key")
Suggestion
Always put keys in constant file for more managed way.
companion object {
val PUT_EXTRA_USER = "PUT_EXTRA_USER"
}
You can get any type of extra data from intent, no matter if it's an object or string or any type of data.
Bundle extra = getIntent().getExtras();
if (extra != null){
String str1 = (String) extra.get("obj"); // get a object
String str2 = extra.getString("string"); //get a string
}
and the Shortest solution is:
Boolean isGranted = getIntent().getBooleanExtra("tag", false);
Just a suggestion:
Instead of using "id" or "name" in your i.putExtra("id".....), I would suggest, when it makes sense, using the current standard fields that can be used with putExtra(), i.e. Intent.EXTRA_something.
A full list can be found at Intent (Android Developers).
We can do it by simple means:
In FirstActivity:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("uid", uid.toString());
intent.putExtra("pwd", pwd.toString());
startActivity(intent);
In SecondActivity:
try {
Intent intent = getIntent();
String uid = intent.getStringExtra("uid");
String pwd = intent.getStringExtra("pwd");
} catch (Exception e) {
e.printStackTrace();
Log.e("getStringExtra_EX", e + "");
}
Pass the intent with value on First Activity:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("uid", uid.toString());
intent.putExtra("pwd", pwd.toString());
startActivity(intent);
Receive intent on second Activity;-
Intent intent = getIntent();
String user = intent.getStringExtra("uid");
String pass = intent.getStringExtra("pwd");
We use generally two method in intent to send the value and to get the value.
For sending the value we will use intent.putExtra("key", Value); and during receive intent on another activity we will use intent.getStringExtra("key"); to get the intent data as String or use some other available method to get other types of data (Integer, Boolean, etc.).
The key may be any keyword to identify the value means that what value you are sharing.
Hope it will work for you.
You can also do like this
// put value in intent
Intent in = new Intent(MainActivity.this, Booked.class);
in.putExtra("filter", "Booked");
startActivity(in);
// get value from intent
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String filter = bundle.getString("filter");
Getting Different Types of Extra from Intent
To access data from Intent you should know two things.
KEY
DataType of your data.
There are different methods in Intent class to extract different kind of data types.
It looks like this
getIntent().XXXX(KEY) or intent.XXX(KEY);
So if you know the datatype of your varibale which you set in otherActivity you can use the respective method.
Example to retrieve String in your Activity from Intent
String profileName = getIntent().getStringExtra("SomeKey");
List of different variants of methods for different dataType
You can see the list of available methods in Official Documentation of Intent.
This is for adapter , for activity you just need to change mContext
to your Activty name and for fragment you need to change mContext to
getActivity()
public static ArrayList<String> tags_array ;// static array list if you want to pass array data
public void sendDataBundle(){
tags_array = new ArrayList();
tags_array.add("hashtag");//few array data
tags_array.add("selling");
tags_array.add("cityname");
tags_array.add("more");
tags_array.add("mobile");
tags_array.add("android");
tags_array.add("dress");
Intent su = new Intent(mContext, ViewItemActivity.class);
Bundle bun1 = new Bundle();
bun1.putString("product_title","My Product Titile");
bun1.putString("product_description", "My Product Discription");
bun1.putString("category", "Product Category");
bun1.putStringArrayList("hashtag", tags_array);//to pass array list
su.putExtras(bun1);
mContext.startActivity(su);
}
I'm trying to create a shortcut for an another application's activity (which is mine too). The shortcut starts the activity correctly, but just one of the two extras is received (or sent, I don't know). See the code below:
//Setting up the intent
Intent intent = new Intent();
intent.putExtra("num", 12);
intent.putExtra("name", "something");
intent.setComponent(new ComponentName
("com.myanother.app","com.myanother.app.MyActivity"));
And the other activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
int num = extras.getInt("num"); //this worked
String name = extras.getString("name"); //this gets null
So, what's wrong? And sorry for my English if I made some mistakes.
Try this:
Bundle bund = new Bundle();
bund.putInt("num",12);
bund.putString("name","hello world");
Intent intent = new Intent();
intent.putExtras(bund);
intent.setComponent(new ComponentName
("com.myanother.app","com.myanother.app.MyActivity"));
Instead of trying to put in multiple extras you can try to pass a bundle as an extra. Look at the first answer of this question. The question is similar and the solution should be work.
How can I send data from one activity (intent) to another?
I use this code to send data:
Intent i=new Intent(context,SendMessage.class);
i.putExtra("id", user.getUserAccountId()+"");
i.putExtra("name", user.getUserFullName());
context.startActivity(i);
First, get the intent which has started your activity using the getIntent() method:
Intent intent = getIntent();
If your extra data is represented as strings, then you can use intent.getStringExtra(String name) method. In your case:
String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");
In the receiving activity
Bundle extras = getIntent().getExtras();
String userName;
if (extras != null) {
userName = extras.getString("name");
// and get whatever type user account id is
}
// How to send value using intent from one class to another class
// class A(which will send data)
Intent theIntent = new Intent(this, B.class);
theIntent.putExtra("name", john);
startActivity(theIntent);
// How to get these values in another class
// Class B
Intent i= getIntent();
i.getStringExtra("name");
// if you log here i than you will get the value of i i.e. john
Add-up
Set Data
String value = "Hello World!";
Intent intent = new Intent(getApplicationContext(), NewActivity.class);
intent.putExtra("sample_name", value);
startActivity(intent);
Get Data
String value;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
value = bundle.getString("sample_name");
}
Instead of initializing another new Intent to receive the data, just do this:
String id = getIntent().getStringExtra("id");
Put data by intent:
Intent intent = new Intent(mContext, HomeWorkReportActivity.class);
intent.putExtra("subjectName", "Maths");
intent.putExtra("instituteId", 22);
mContext.startActivity(intent);
Get data by intent:
String subName = getIntent().getStringExtra("subjectName");
int insId = getIntent().getIntExtra("instituteId", 0);
If we use an integer value for the intent, we must set the second parameter to 0 in getIntent().getIntExtra("instituteId", 0). Otherwise, we do not use 0, and Android gives me an error.
If used in a FragmentActivity, try this:
The first page extends FragmentActivity
Intent Tabdetail = new Intent(getApplicationContext(), ReceivePage.class);
Tabdetail.putExtra("Marker", marker.getTitle().toString());
startActivity(Tabdetail);
In the fragment, you just need to call getActivity() first,
The second page extends Fragment:
String receive = getActivity().getIntent().getExtras().getString("name");
If you are trying to get extra data in fragments then you can try using:
Place data using:
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER);
Get data using:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getArguments().getInt(ARG_SECTION_NUMBER);
getArguments().getString(ARG_SECTION_STRING);
getArguments().getBoolean(ARG_SECTION_BOOL);
getArguments().getChar(ARG_SECTION_CHAR);
getArguments().getByte(ARG_SECTION_DATA);
}
Kotlin
First Activity
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("key", "value")
startActivity(intent)
Second Activity
val value = getIntent().getStringExtra("key")
Suggestion
Always put keys in constant file for more managed way.
companion object {
val PUT_EXTRA_USER = "PUT_EXTRA_USER"
}
You can get any type of extra data from intent, no matter if it's an object or string or any type of data.
Bundle extra = getIntent().getExtras();
if (extra != null){
String str1 = (String) extra.get("obj"); // get a object
String str2 = extra.getString("string"); //get a string
}
and the Shortest solution is:
Boolean isGranted = getIntent().getBooleanExtra("tag", false);
Just a suggestion:
Instead of using "id" or "name" in your i.putExtra("id".....), I would suggest, when it makes sense, using the current standard fields that can be used with putExtra(), i.e. Intent.EXTRA_something.
A full list can be found at Intent (Android Developers).
We can do it by simple means:
In FirstActivity:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("uid", uid.toString());
intent.putExtra("pwd", pwd.toString());
startActivity(intent);
In SecondActivity:
try {
Intent intent = getIntent();
String uid = intent.getStringExtra("uid");
String pwd = intent.getStringExtra("pwd");
} catch (Exception e) {
e.printStackTrace();
Log.e("getStringExtra_EX", e + "");
}
Pass the intent with value on First Activity:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("uid", uid.toString());
intent.putExtra("pwd", pwd.toString());
startActivity(intent);
Receive intent on second Activity;-
Intent intent = getIntent();
String user = intent.getStringExtra("uid");
String pass = intent.getStringExtra("pwd");
We use generally two method in intent to send the value and to get the value.
For sending the value we will use intent.putExtra("key", Value); and during receive intent on another activity we will use intent.getStringExtra("key"); to get the intent data as String or use some other available method to get other types of data (Integer, Boolean, etc.).
The key may be any keyword to identify the value means that what value you are sharing.
Hope it will work for you.
You can also do like this
// put value in intent
Intent in = new Intent(MainActivity.this, Booked.class);
in.putExtra("filter", "Booked");
startActivity(in);
// get value from intent
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String filter = bundle.getString("filter");
Getting Different Types of Extra from Intent
To access data from Intent you should know two things.
KEY
DataType of your data.
There are different methods in Intent class to extract different kind of data types.
It looks like this
getIntent().XXXX(KEY) or intent.XXX(KEY);
So if you know the datatype of your varibale which you set in otherActivity you can use the respective method.
Example to retrieve String in your Activity from Intent
String profileName = getIntent().getStringExtra("SomeKey");
List of different variants of methods for different dataType
You can see the list of available methods in Official Documentation of Intent.
This is for adapter , for activity you just need to change mContext
to your Activty name and for fragment you need to change mContext to
getActivity()
public static ArrayList<String> tags_array ;// static array list if you want to pass array data
public void sendDataBundle(){
tags_array = new ArrayList();
tags_array.add("hashtag");//few array data
tags_array.add("selling");
tags_array.add("cityname");
tags_array.add("more");
tags_array.add("mobile");
tags_array.add("android");
tags_array.add("dress");
Intent su = new Intent(mContext, ViewItemActivity.class);
Bundle bun1 = new Bundle();
bun1.putString("product_title","My Product Titile");
bun1.putString("product_description", "My Product Discription");
bun1.putString("category", "Product Category");
bun1.putStringArrayList("hashtag", tags_array);//to pass array list
su.putExtras(bun1);
mContext.startActivity(su);
}