Bundle null onActivityResult - java

I have tried a couple of different solutions already given but none works as everything seems to work fine in another activity that I am returning a result from, here is the code.
My Main activity where City activity is called:
//this method gets called on a button click and it works as other activity shows up
public void getCity(View v){
Intent intent = new Intent(getApplicationContext(), City.class);
startActivityForResult(intent,1);
}
//receiving the data the first data is ok but the second one is null although doing the same thing in both files
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(resultCode == RESULT_OK && data != null && requestCode==0){
initializeUI(data.getExtras());
}
if(resultCode == RESULT_OK && data != null && requestCode==1){
//data is null here
}
super.onActivityResult(requestCode, resultCode, data);
}
My City Activity
public void addInput(View v){
Bundle bundle = new Bundle();
EditText cityBox = (EditText) findViewById(R.id.cityInput);
String cityName = cityBox.getText().toString();
Intent resultIntent = new Intent();
try {
EditText longBox = (EditText) findViewById(R.id.longitudeInput);
String longitude = longBox.getText().toString();
double longi = Double.parseDouble(longitude);
bundle.putDouble("LONGITUDE", longi);
EditText latBox = (EditText) findViewById(R.id.latitudeInput);
String latitude = latBox.getText().toString();
double lati = Double.parseDouble(latitude);
bundle.putDouble("LATITUDE", lati);
} catch (NumberFormatException e){
}
bundle.putString("CITY_NAME", cityName);
resultIntent.putExtra("DATA",bundle);
setResult(RESULT_OK, resultIntent);
finish(); //calling finish just in case tried without finish aswell
onBackPressed(); //calling onBackPressed tried without it as well doesn't work
}
Any help would be appreciated.

try this:
Then, in B, I do:
Intent intent = getIntent();
intent.putExtra("Date",dateSelected);
setResult(RESULT_OK, intent);
finish();
And, in A:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode==RESULT_OK && requestCode==1){
Bundle MBuddle = data.getExtras();
String MMessage = MBuddle .getString("Date");
}
}
the problem i have found in your code is use of wrong requestCode you are passing 1 as result code in startActivityForResult and in onActivityResult you are checking requestCode==0 means intializeUI etc. All you need to do is use this below code.
if(resultCode == RESULT_OK && data != null && requestCode==1){
//everything works fine
initializeUI(data.getExtras());
}
hope this will help you.

I recently faced same issue in my case. What i was doing wrong was that i was using startActivity() by mistake with startActivityForResult() then in child activity i was calling onBackPress() and finish() before setting the result. I can suggest you that you make sure you set the result before finish() the activity and make sure you're starting activity with startActivityForResult().
Here is snippet from my code.
setResult(RESULT_OK, getIntent().putExtra("country", "some result"));
And i made sure the above written code get excuted before onBackPressed() and onFinish() and it worked for me.
Hope this will help you.

Related

How to pass Parceable in an intent?

I am having a problem receiving the extras that are coming in with my intent from a different activity. When I run the code and use break points at the line where the object is actually being created I can see it being created using the debugger. I can see the object being put in the putExtra() function.
Upon arrival at the resulting activity though using the function getParceableExtra() always returns null upon receiving the intent.
//In the receving activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 1){
if(resultCode == Activity.RESULT_OK){
Intent extras = getIntent();
if(extras != null){
Goals addToList = extras.getParcelableExtra(GOAL_PASSING);
if(addToList != null)
mGoal.add(addToList);
}
}
}
}
//In the sending activity
incrementMGoal = (findViewById(R.id.create_goal_button));
incrementMGoal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent returnIntent = new Intent();
Goals newGoal = new Goals(GT,GD,GDueDate);
returnIntent.putExtra(MainActivity.GOAL_PASSING,newGoal);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});
I expect when I create the intent to pass putExtra and then receive it to be put in a array. I am just receiving null however.
The Problem is that your code takes the wrong intent and not of the onActivityResult parameter data
Goals addToList = data.getParcelableExtra(GOAL_PASSING);
so instead you should do this
Goals addToList = data.getParcelableExtra(GOAL_PASSING);

Android activity does not finish and does not launch previous activity

I have 2 Activities the main activity I have called Map activity and the second that I have called Question activity when the app launch the Map activity is shown then you click the play button and the Question activity is launched with StartActivityForResult(), then when you have answered the question right the Question activity should be destroyed and created again and check the number of right answered questions and change the layout. then if you have scored 5 then Question activity should be destroyed and the Map activity will be shown but this is not happen. Here is my code
Map.class
public void OnClick_Question(View v){
Intent i = new Intent(MapActivity.this, QuestionActivity.class);
startActivityForResult(i, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(SharedPrefs.TAG, "OnActivityResult Entry");
if (resultCode == RESULT_OK) {
Log.d(SharedPrefs.TAG, "MAPActivity OnActivityResult Entry resultCode");
AssignContentView(requestCode);
}
}
Question.class
public void Next() {
Intent i = new Intent(this, QuestionActivity.class);
startActivity(i);
finish();
}
public void LevelUp() {
Log.e(SharedPrefs.TAG, "LevelUp");
super.finish();
finish(); /*This don't happen*/
}
When I see LogCat shows me this message:
09-01 11:23:03.911 901-1485/? W/ActivityManager﹕ startActivity called from finishing ActivityRecord{43386100 u0 com.example.gbb/.QuestionActivity t50 f}; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { cmp=com.example.gbb/.QuestionActivity }
Why I am doing this, is because I want to change the layout of Map Activity depending in your score.
What I'm doing wrong?
I had the same problem. I solved it by adding android:launchMode="singleTask" in the Android Manifest. Hope it works for you too.
I solved this by turning off Instant Run and re-running my code.
You can try the below code:
We need to keep the request code to be in global and static variable to access in onActivityResult method
private static final int REQUEST_CODE = 111;
public void OnClick_Question(View v){
Intent i = new Intent(MapActivity.this, QuestionActivity.class);
startActivityForResult(i, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.d(SharedPrefs.TAG, "OnActivityResult Entry");
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE:
Log.d(SharedPrefs.TAG, "MAPActivity OnActivityResult Entry resultCode");
AssignContentView(requestCode);
break;
default:
break;
}
}
}
I think there is no problem in your Map class, the problem is in second class.i.e., you need to set the RESULT_OK in that intent and then you can check if (resultCode == RESULT_OK) in onActivityResult method. Below you can find the code for that
Intent intent = new Intent();
setResult(activity.RESULT_OK, intent);
finish();
Hope this is helpful:)

finishAndRemoveTask() available on API 21

I would terminate my app and cancel it from the list of recent task.
finishAndRemoveTask() is available only on API 21.
What should I use on API lower than 21??
Make an intent to the first activity in the stack and finish the current activity:
Intent intent = new Intent(this, FirstActivity.class);
intent.putExtra(EXTRA_FINISH, true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
And, in the onResume method of the FirstActivity, something like this to finish the last activity in the stack (and hopefully removing the app from the recent apps list):
if (getExtras() != null && getIntentExtra(EXTRA_FINISH, false)) {
finish();
}
I had a similar use case where I needed to finish all activities. Here is one way to do it without finishAndRemoveTask().
Make all your activities extend a base class with the following things in it:
private Boolean mHasParent = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {
mHasParent = extras.getBoolean("hasParent", false);
}
}
// Always start next activity by calling this.
protected void startNextActivity(Intent intent) {
intent.putExtra("hasParent", true);
startActivityForResult(intent, 199);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 199 && resultCode == FINISH_ALL) {
finishAllActivities();
}
}
protected void finishAllActivities() {
if (mHasParent) {
// Return to parent activity.
setResult(FINISH_ALL);
} else {
// This is the only activity remaining on the stack.
// If you need to actually return some result, do it here.
Intent resultValue = new Intent();
resultValue.putExtra(...);
setResult(RESULT_OK, resultValue);
}
finish();
}
Simply call finishAllActivities() in any activity, and all the activities will unwind. Ofcourse if you don't care what result the last activity returns, the code can be made much simpler.

Change object using intent and startActivityForResult

I have an object called Contact which contains an arraylist of type Transactions.
I pass the selected contact (from a listview in activity1) to activity2 using intent and startActivityForResult.
In the second activity I create a new Transaction object and add it to the passed Contact's arraylist of transactions, return it using setResult and retrieve it using onActivityResult
My problem is that the original Contact object doesn't get the new Transaction.
Sending intent:
Intent intent = new Intent(this, AddTransactionActivity.class);
intent.putExtra("contact", selectedContact);
startActivityForResult(intent, 1);
recieving intent:
Bundle b = getIntent().getExtras();
if(b != null) {
recievedContact = (Contact)b.getParcelable("contact");
}
Sending back result:
recievedContact.addTransaction(transaction);
Intent intent = new Intent(this, Contacts_activity.class);
intent.putExtra("contact", recievedContact);
setResult(Activity.RESULT_OK, intent);
finish();
startActivity(intent);
Retrieving result:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1) {
if(resultCode == Activity.RESULT_OK) {
Bundle b = data.getExtras();
if(b != null) {
selectedContact = (Contact)b.getParcelable("contact");
}
} else if (resultCode == 0) {
}
}
}
edit: I put a Log.v("result test", "success"); in onActivityResult(), it doesnt show in logcat so it seems my onActivityResult method is never called.

Refresh text EditText after finish()

I created a 'New File' activity using
startActivityForResult(new Intent(MainActivity.this, NewFile.class),1);
The NewFile activity lets users set certain options for their text file then after clicking a button a string is saved to a static variable in my StringBuilder class and finish(); is called. How can I load this new string into the MainActivity's EditText? onCreate() is only called when the activity is first created right?
Do it on onResume or onActivityResult. It would be ideal though onActivityResult since you've used startActivityForResult, before finishing the other activity you set the setResult(int resultCode, Intent data) if you have intent to sent back or if none setResult(int resultCode). I think it is better to put the string which will be used to update your EditText as extra in the intent, then set the text using that string in onActivityResult method.
#Override
protected void onResume() {
super.onResume();
et.setText(DocumentBuilder.docText);
}
in your class NewFile.java :
String strName = "toto";
Intent intent = new Intent();
intent.putExtra("name", "toto");
setResult(1, intent);
finish();
in your MainActivity.java :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// Handle successful result
String name = intent.getStringExtra("name");
editText.setText(name);
}
}
}
refer this tutorial for more explanations

Categories