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
Related
I must be doing something very silly, but even after a whole day, I cant call onActivityResult on MainActivity from another activity.
In my AuxActivity, I have
adapter.setOnItemClickListener(new CityListAdapter.ClickListener() {
#Override
public void onItemClick(View v, int position) {
City city =adapter.getCityAtPosition(position);
// Toast.makeText(getApplicationContext(),
// city.getCity()+"\n"+city.getLatitude()+"\n"+city.getLongitude(),
// Toast.LENGTH_LONG).show();
Double Lat = city.getLatitude();
Double Long = city.getLongitude();
Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
mainIntent.putExtra("Lat", Lat);
mainIntent.putExtra("Long", Long);
mainIntent.putExtra("DbResultCode", Db_ACTIVITY_REQUEST_CODE);
startActivityForResult(mainIntent, Db_ACTIVITY_REQUEST_CODE);
finish();
}
});
In MainActivity, I am doing
// Inside onCreate
// Check Intent from Db
Intent intent = getIntent();
if (intent != null) {
Lat = intent.getDoubleExtra("Lat", 0.0);
Long = intent.getDoubleExtra("Long", 0.0);
DbResultStat = intent.getIntExtra("DbResultCode", 0);
}
I am getting values of Lat and Long properly here. But, then,
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(this, ""+requestCode, Toast.LENGTH_LONG).show();
if (requestCode == DbActivity.Db_ACTIVITY_REQUEST_CODE){
if (resultCode==RESULT_OK){
setupViewPager();
}
}
This part is never called. I am still learning android, using the codelabs. So, anything new is giving me a lot of headache.
What I am doing wrong here?
Try these two steps:-
1)Pass Context of Main Activity instead of Application:
Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
to:
Intent mainIntent = new Intent(this, MainActivity.class);
Then Run
2)Remove Super from override method of onActivityResult:
super.onActivityResult(requestCode, resultCode, data);
Then Run
Hope it Helps
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.
I want to receive data in previous Activity from next activity like (1 <--- 2 ). I tried but data is not received from second to first activity .
This is First Cativity
Intent i = new Intent(CustomActionActivity.this, Edit_Post.class);
i.putExtra("ActivityId", getItemActivity);
i.putExtra("Vessel", strVesselName);
i.putExtra("HashTag", strHashTag);
i.putExtra("RemarkTitle", strRemark);
i.putExtra("ShortRGN", strShortTypeRGN);
i.putExtra("VessId", strvesselid);
startActivity(i);
This is second Activity
Intent intent = getIntent();
strActivityId = intent.getStringExtra("ActivityId");
strVesselName = intent.getStringExtra("Vessel");
strHashTag = intent.getStringExtra("HashTag");
strRemark = intent.getStringExtra("RemarkTitle");
strShortRGN = intent.getStringExtra("ShortRGN");
strVessId = intent.getStringExtra("VessId");
img_AddPostAudio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Edit_Post.this, EditRecord.class);
i.putExtra("EditVesselId", strVessId);
i.putExtra("EditActivityId" , strActivityId);
startActivity(i);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_CODE)
{
String audioString=data.getStringExtra("AUDIO_STRING");
Log.e(" audioString "," = "+audioString);
}
}
This is Third Activity
Intent intent = getIntent();
vesselId = intent.getStringExtra("EditVesselId");
strActivityId = intent.getStringExtra("EditActivityId");
Intent intent=new Intent(EditRecord.this, Edit_Post.class);
intent.putExtra("AUDIO_STRING",newAudioFile);
setResult(REQUEST_CODE, intent);
finish();
do this when you are calling the second activity
Intent i = new Intent(CustomActionActivity.this, Edit_Post.class);
i.putExtra("HashTag", strHashTag);
startActivityForResult(i, REQUEST_CODE);
Now you need to set the result what you want on CustomActionActivity
e.g.
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(REQUEST_CODE,intent);
finish();
Now you will get this data to the your first activity
e.g.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_CODE)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}
let me know in case of any issue
Use SharedPreferences
Store the data in a SharedPreference and access it in from the SharedPreference in the other activity.
Add the data to the SharedPreference in the onStop for the 2nd Activity and access it in the onCreate of the other Activity
#Override
public void onStop(){//Where you wish to insert data
SharedPreferences data=getSharedPreferences(PREFS_FILE,0);
SharedPreferences.Editor editor= count.edit();
editor.put("data","DATA");
editor.apply();
super.onStop();
}
in onCreate() of the other Activity:
SharedPreferences data = getApplicationContext().getSharedPreferences(PREFS_FILE, 0);
String dataString=data.get("Data",0);
Hope this helps. Cheers.
I am doing this way its works for me:
Activity 2:
public void onBackPressed() {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("MESSAGE", strtext + "");
setResult(2, intent);
if (isclose) {
finish();
} else {
super.onBackPressed();
}
}
}
Activity1:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String sSuName = data.getStringExtra("MESSAGE");
//txtfavouratecount.setText(sSuName);
}
in Your onclick listener
Intent itemintent = new Intent(context,your target Activity.class);
Bundle b = new Bundle();
b.putStringArray("iarray", Qtag);
b.putInt("mflag", 0);
itemintent.putExtra("android.intent.extra.INTENT", b);
startActivityForResult(itemintent,2);
I would do something like this:
In Activity A:
private static final int REQUEST_CODE = 9001;
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, REQUEST_CODE);
In Activity B:
Intent data = new Intent();
data.putExtra("key", parameter);
setResult(CommonStatusCodes.SUCCESS, data);
finish();
And finally, in the Activity A, receive result:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == CommonStatusCodes.SUCCESS) {
if (data != null) {
String result = data.getStringExtra("key");
}
} else {
//Error to receive data
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Good luck!
I have X, Z activities and Y class. Y is view of activity X and called activity for result from X to Z..its not going to onActivityResult in X.
Activity X:
SetContentView(Y);
and
Intent i=new Intent(x.this,z.class);
startActivityforResult(i,100);
onActivityResult method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==100)
{
Log.e("Message from Z","Message");
}
}
Activity Z:
setResult(RESULT_OK, (new Intent()).setAction("close"));
this.finish();
How to solve this??
You are creating a new intent; you dont need to do that.
This should work:
setResult(Activity.RESULT_OK);
finish();
Also as a recommendation dont use hard coded request code ( in your case 100). Declare a private static final int variable and use it when starting the activity for result (startActivityforResult) and when accepting the result on onActivityResult method.
Use setResult() like this:
Intent i = getIntent(); //gets the intent that called this intent
setResult(Activity.RESULT_OK, i);
Add your intent as one parameter of setResult()
Try this :
in onActivityResult() update the code
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==100)
{
if(resultCode == RESULT_OK) {
Log.e("Message from Z","Message");
}
}
}
Activity Z:
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
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).