I open a RemoteInput via intent but need to pass some additional data. I eventually retrieve it in the onActivityResult
Launch:
Bundle d = new Bundle();
d.putString("chatID", id);
RemoteInput remoteInput = new RemoteInput.Builder("remote_input")
.setLabel("Send to "+name)
.addExtras(d)
.build();
RemoteInput[] remoteInputs = new RemoteInput[]{remoteInput};
Intent intent = new Intent(RemoteInputIntent.ACTION_REMOTE_INPUT);
intent.putExtra(RemoteInputIntent.EXTRA_REMOTE_INPUTS, remoteInputs);
intent.putExtra("chatID", id);
intent.putExtra("asd", "das");
startActivityForResult(intent, 0);
Retrieve:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println(data);
System.out.println(data.getExtras());
System.out.println(data);
System.out.println(data.getExtras().toString());
if (resultCode == RESULT_OK && requestCode == 0) {
System.out.println(data);
Bundle results = RemoteInput.getResultsFromIntent(data);
String text = results.getCharSequence("remote_input").toString();
System.out.println(" >");
Bundle c = data.getExtras();
//Object cd = data.getExtras().get("remote_input_types");
Object cd = results.get("remote_input");
System.out.println(cd);
System.out.println(cd.getClass().getName());
System.out.println(c);
System.out.println(c.keySet());
System.out.println(" <");
Set<String> keys = c.keySet();
Iterator<String> it = keys.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
None of what I try brings up "chatID", it simply seems to be missing from the keys. How else am I supposed to get the extras?
I believe the problem is how you are sending or launching the data.
Try using putExtraS() instead of putExtra(). PutExtras() is used to hold object of bundle class.
Launch:
Bundle bundle = new Bundle();
bundle.putString(Key, value);
Intent intent = new Intent(context, className);
intent.putExtras(bundle);
intent.setFlags(intFlag);
startActivityForResult();
Retrieve:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == 0) {
Bundle dataBundle = data.getExtras();
//Use data bundle
}
}
Related
hey guys i wanted to back to onActivityResult in previous activity
here is my code
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(DetailPhotoMedical.this, MedicalClaim.class);
Bundle extras = new Bundle();
// i.putExtra("photo", "");
extras.putString("photo","");
extras.putString("photoNo",photoNo);
i.putExtras(extras);
setResult(1000, i);
finish();
}
and i want to back to previous activity in onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==1000)
{
Intent ii = getIntent();
Bundle extras = ii.getExtras();
photoFromDetail = extras.getString("photo");
photoNo = extras.getString("photoNo");
if (photoNo=="photo1")
{
strPhoto1="";
ivAttachment1.setImageResource(R.drawable.ic_menu_camera);
}
}
here is code to intent the 2nd acitvity
Intent i = new Intent(MedicalClaim.this, DetailPhotoMedical.class);
/* i.putExtra("photo", strPhoto1);*/
Bundle extras = new Bundle();
extras.putString("photo", strPhoto1);
extras.putString("photoNo","photo1");
i.putExtras(extras);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
but the OnActivityResult didnt catch my resultcode
can you tell me whereis the wrong?
thx
For calling 2nd activity using intent, use
startActivityForResult(i)
instead of
startActivity(i)
dont use
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
unless you know the consequences of using it and for getting intent in onActivityResult use
data
variable passed as a parameter in
onActivityResult(requestCode, resultCode, data)
something like this
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==1000)
{
Bundle extras = data.getExtras();
photoFromDetail = extras.getString("photo");
photoNo = extras.getString("photoNo");
if (photoNo=="photo1")
{
strPhoto1="";
ivAttachment1.setImageResource(R.drawable.ic_menu_camera);
}
}
Your question is not clear.
You should describe more clearly about your case:
OnActivityResult is not called.
OnActivityResult is called, but the resultCode is not 1000.
OnActivityResult is called, resultCode = 1000 but you cannot get extra data.
For 2 first cases please make sure that you called startActivityForResult().
For 3rd case please remove this line Intent ii = getIntent(); and use the Intent which is input parameter of onActivityResult.
The code are you using for catch the result is wrong, you should use something like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK) //Here is where you need to do the change
{
Intent ii = getIntent();
Bundle extras = ii.getExtras();
photoFromDetail = extras.getString("photo");
photoNo = extras.getString("photoNo");
if (photoNo=="photo1")
{
strPhoto1="";
ivAttachment1.setImageResource(R.drawable.ic_menu_camera);
}
}
Because you compare a wrong parameter you always has false in your if
Hope this help you
finally it solve,
just using statActivityForResult()
and remove i.getintet and use data.getExtras
Its happening because you are getting data from different Intent.
This,
if(resultCode==100){
Intent ii = getIntent();
Bundle extras = ii.getExtras();
photoFromDetail = extras.getString("photo");
photoNo = extras.getString("photoNo");
if (photoNo=="photo1") {
strPhoto1="";
ivAttachment1.setImageResource(R.drawable.ic_menu_camera);
}
}
Sohuld be like this.
if(resultCode==100){
Bundle extras = data.getExtras();
photoFromDetail = extras.getString("photo");
photoNo = extras.getString("photoNo");
if (photoNo=="photo1") {
strPhoto1="";
ivAttachment1.setImageResource(R.drawable.ic_menu_camera);
}
}
Where you can see data in an intent given in onResultActivity's parameters.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
.
.
.
}
EDIT : make sure you are using StartActivityForResult(); for calling Activity.
I hope this will help you out.
Suppose I have a main activity A and two other activities B and C.
A launches intent B and at some point B launches intent C.
Then C sets setResult(...) and finish()'s, as does B, finally ending at onActivityResult(...) in A.
Is this allowed; will it work?
Yes, it will work. Just finish activity B when recieve the result from C. However the result from activity C will not be propagated when finishing B, you will have to set it manually if necessary.
In activity A:
Intent intent = new Intent(this, B.class);
startActivityForResult(intent, 1);
....
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1){
if (resultCode == RESULT_OK){
Bundle bundle = data.getExtras();
String code = "";
try{
code = bundle.getString("code");
} catch (Exception e){
e.printStackTrace();
}
if (!code.equals("")){
//do something
}
}
}
In Activity B:
Intent intent = new Intent(this, C.class);
startActivityForResult(intent, 1);
...
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1){
if (resultCode == RESULT_OK){
Bundle bundle = data.getExtras();
String code = "";
try{
code = bundle.getString("code");
} catch (Exception e){
e.printStackTrace();
}
if (!code.equals("")){
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("code", code);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
}
}
Then in Activity C at some point:
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("code", code);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
I hope this can help you.
Yes it will work. Here's an overview of the process you probably just need:
Activity A {
startActivityForResult() // start activity B
onActivityResult() // receive B's result
}
Activity B {
getIntentFromA()
startActivityForResult(); // start activity C
onActivityResult() {
// receive C's result
setResult(c's result); <-- this will pass back to A
}
}
Activity C {
getIntentFromB()
setResult(c's result); <-- this will pass back to B
}
My source code is as follows:
in MainActivity:
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case LOGIN_ACTIVITY_REQUEST_CODE:
{
if(resultCode == RESULT_OK){
Bundle bd = new Bundle();
bd = data.getBundleExtra("bundle");
}
}
default:
{
Log.d(DEBUG_ACTIVITY_CLASS_NAME, "ERROR : onActivityResult - unknown activity code");
}
}
}
And second activity's code is:
public void returnToGameActivity(Bundle bundle) {
Intent intent = new Intent();
intent.putExtra("bundle", bundle);
this.setResult(RESULT_OK, intent);
finish();
}
But i can't receive data 'bd' of Bundle type in onActivityResult() in MainActivity. Why?
But in this case, i can get data of bd:
in second activity,
putExtra("string", "test string");
and in MainActivity,
String str = getStringExtra("string");
Why i can't get data in bundle type?
To transfer a Bundle element from one activity to other,use the following code:
Intent i=new Intent(First.this,Second.class);
//i.putExtra("mylist",amt);
Bundle b = new Bundle();
b.putSerializable("bundleinterest", (Serializable) amtint);
b.putSerializable("bundleobj", (Serializable) amt);
i.putExtras(b);
startActivity(i);
In Second.class:
Bundle bn = new Bundle();
bn = getIntent().getExtras();
getobj = new ArrayList<CompoundAmount>();
getinterestobj=new ArrayList<CompoundIntAmount>();
getobj = (ArrayList<CompoundAmount>) bn.getSerializable("bundleobj");
getinterestobj = (ArrayList<CompoundIntAmount>) bn.getSerializable("bundleinterest");
Explanation:
Use Serializable or Parcelable interfaces while transferring Bundles.
Your set-get class must implements Serializable as:
public class CompoundIntAmount implements Serializable {
private final String amt;
public CompoundIntAmount(String amt){
this.amt = amt;
}
public String getIntAmount() {
return amt;
}
}
Here CompoundAmount and CompoundIntAmount are custom set-get classes which must implement Serializable(like the one above).
I am working on an app with 3 activites. Activity 3 opens from 2, and 2 opens from 1. I would like to get two numbers that the user enters in activity 3 and handle them in activity 2.
This is the code I am currently using in activity 3 to bundle my numbers to be returned to #2:
#Override
protected void onPause(){
super.onPause();
Bundle bundle = new Bundle();
bundle.putInt("param1", num1);
bundle.putInt("param2", num2);
Intent i = new Intent(this, Activity2.class);
i.putExtras(bundle);
setResult(ACTIVITY_END, i);
finish();
}
And then in Activity #2:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
Bundle bundle = this.getIntent().getExtras();
int num1 = bundle.getInt("param1"));
int num2 = bundle.getInt("param2");
//do something with ints
}
However, no matter what numbers are sent from #3 to #2, num1 and num2 are always 0.
Any ideas?
Thanks a lot!
Try this user,
#Override
protected void onPause(){
super.onPause();
Bundle bundle = new Bundle();
bundle.putInt("param1", num1);
bundle.putInt("param2", num2);
Intent i = new Intent(this, Activity2.class);
i.putExtra("my_bundle", bundle);
setResult(ACTIVITY_END, i);
finish();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
Bundle bundle = data.getBundleExtra("my_bundle");
int num1 = bundle.getInt("param1"));
int num2 = bundle.getInt("param2");
//do something with ints
}
You should put params directly in the intent
i.putExtra("param1", num1);
i.putExtra("param2", num2)
to retrieve these values use
int param1 = intent.getIntExtra("param1", -1);
link
I have this code:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setData(ContactsContract.Contacts.CONTENT_URI);
intent.putExtra(EXTRA_ONLINE_ID, (String) v.getTag());
startActivityForResult(intent, PICK_CONTACT);
Then on response:
public void onActivityResult(int reqCode, int resultCode, Intent data) {
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
try {
Uri contactData = data.getData();
String onlineid = data.getStringExtra(EXTRA_ONLINE_ID);
} catch (Exception e) {
e.printStackTrace();
}
}
break;
}
super.onActivityResult(reqCode, resultCode, data);
}
the onlineid variable is null. How can I pass a value and then to receive it back?
EDIT
I even tried,
Bundle extras = data.getExtras(); // returns null
This is done by design; system activities will not send back the extras with which they're called, so you have to manage the data elsewhere.
Luckily, the resultCode parameter is fully controlled by yourself, which means that you can use it to index your data.
private final int PICK_CONTACT = 0;
private Bundle[] myDataTransfer = { null };
...
Bundle myData = new Bundle();
myData.putString(EXTRA_ONLINE_ID, (String) v.getTag());
myDataTransfer[PICK_CONTACT] = myData;
// create intent and all
startActivityForResult(intent, PICK_CONTACT);
...
public void onActivityResult(int reqCode, int resultCode, Intent data) {
if (resultCode == PICK_CONTACT) {
Bundle myData = myDataTransfer[resultCode];
String onlineid = myData.getString(EXTRA_ONLINE_ID);
}
}
I'm not a Java programmer, there must be a nicer way to implement a map of Bundles, but this works :)
ok Check if your Activity android:launchMode is configured as SingleTask or SingleInstance! that must be the problem :)
The EXTRA_ONLINE_ID field will have to be set in the activity that you launched using setResult. If it's not setting that value in the returned Intent (which is different from what you sent) then you will get a null value.
I was running into some problems with this as well.
Instead of this line
intent.putExtra(EXTRA_ONLINE_ID, (String) v.getTag());
Try
intent.putExtra(EXTRA_ONLINE_ID, "" + v.getTag());