activity A should be closed after ActivityB but sometimes stays - java

It's a bit weird but maybe someone has an idea.
Activity A opens Activity B. In Activity B i press a button and it should be closed
returning to Activity A and then close it as well.
However, sometimes (1 to 7 times maybe) Activity B closes but Activity A remains.
Activity A
public void Foo() {
if (bIsVerifyClicked)
{
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivityForResult(intent, 1000);
}
else
{
setResult(RESULT_OK);
finish();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(Logger.TAG, "=============requestCode: " + requestCode + " resultCode: " + resultCode);
if (resultCode == RESULT_OK)
{
setResult(RESULT_OK);
finish();
}
super.onActivityResult(requestCode, resultCode, data);
}
Activity B
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(Logger.TAG, "ActivityB ==== finish()");
setResult(RESULT_OK);
finish();
}
});
i thought maybe there is a memory leak in Activity B and thus the onResult get lost or something,
but it's java and i don't see anything strange.
do you have any lead what should i look for?
or how can i better debug this?

I am not able to figure out the exact issue.
But in your code i can see onActivityResult() of Activity A, no need of
setResult(RESULT_OK);
You can directly finish() activity A.

Related

extra value not passed from activity using onActivityResult to fragment

Update
Activity A ==> host MyFragment and from here,user started ==> ActivityB from here I need to get some value from this activty for example value integer and some String to MyFragment that hosted in ActivityA
I've MyFragment that start the activityB like this :
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
startActivityForResult(intent,addComment);
and then when user start the activty and finish it I'm trying to pass some value from that ActivityB to MyFragment like this :
#Override
public void onBackPressed() {
super.onBackPressed();
//set ok result before finish the activity
Intent returnIntent = new Intent();
returnIntent.putExtra("idBadgeComment", idBadgeComment);
returnIntent.putExtra("totalCommentInserted", totalCommentInserted);
setResult(RESULT_OK, returnIntent);
finish();
}
and I've implement onActivityResult in MyFragment :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "Result from News Comment ");
if(resultCode== Activity.RESULT_OK){
Log.i(TAG, "Result from News OK");
if(requestCode==addComment){
Log.i(TAG, "Result ReqCode Oke");
int idBadgeComment = data.getExtras().getInt("idBadgeComment");
int totalCommentInserted = data.getExtras().getInt("totalCommentInserted");
Log.i(TAG, "idComment: "+idBadgeComment);
Log.i(TAG, "Total Comment Inserted: "+totalCommentInserted);
}
}
}
also I've called onActivityResult in ActivityA that hosted MyFragment :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/**
* call below code to get the result on the fragment
*/
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.viewpager);
fragment.onActivityResult(requestCode, resultCode, data);
}
when I try the code above I only get Log.i(TAG, "Result from News Comment "); and it didn't get the RESULT_OK value and the Intent data is null, can someone please pointed out where did I go wrong? and how to pass the value from activity to fragment ? or maybe a better way instead of using onActivityResult?
You can try putting super.onBackPressed(); as the last line since RESULT_CANCELED is returned on back press.
Just startActivity by activity.
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
getActivity().startActivityForResult(intent,addComment);
Or put your onActivityResult to fragment.

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.

Android: setResult not working

Scenario: I have a MainActivity.java, OtherPageForFragments.java and a fragment which is on OtherPageForFragments.java
In MainActivity.java, I have written the following code to start an activity and get result in
onActivityResult(int requestCode, int resultCode, Intent data)
is
startActivityForResult(new Intent(this, OtherPageForFragments.class),REQUEST_CODE_MAP);
In the onDestroy() of the fragment class, i have written this:
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
mlocManager.removeUpdates(this);
Intent intent = new Intent();
intent.putExtra("Latitude", passLatLng.latitude);
intent.putExtra("Longitude", passLatLng.longitude);
getActivity().setResult(Activity.RESULT_OK, intent);
getActivity().finish();
}
Now, I want my result in the MainActivity class. So, i have written the following code in the onActivityResult method:
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_MAP)
{
tost("2");
double lat=data.getExtras().getDouble("Latitude");
double lng=data.getExtras().getDouble("Longitude");
tost(lat + " -- " + lng);
}
The Problem: the resultCode getting returned is not Activity.RESULT_OK and the Intent I am getting is null.
What to do? Thanks
getActivity().setResult(Activity.RESULT_OK, intent);
getActivity().finish();
this code shouldn't be in onDestroy. onDestroy happens after the activity is already finished, and onActivityResult was called.
this code needs to in the code that closes the activity/fragment,
like on back key pressed, or a close button onClick
Try this:
Intent data = new Intent();
intent.putExtra("Latitude", passLatLng.latitude);
intent.putExtra("Longitude", passLatLng.longitude);
if (getParent() == null) {
setResult(Activity.RESULT_OK, data);
} else {
getParent().setResult(Activity.RESULT_OK, data);
}
getActivity().finish();
maybe you need clarify the launch modes for both activities. normally they should be "standard", if there is "singleTop" ... attributes in your activity manifest file. you need pay more attention.
For me removing flag Intent.FLAG_ACTIVITY_NEW_TASK for lauch intent resolved the issue
The method finish() is called before setResult()
Try overiding onBackPressed() method like this
override fun onBackPressed() {
setResult(RESULT_OK)
super.onBackPressed()
}

onActivityResult not called

There are many question here from people having the exact same problem as I, and I've looked though a million, tried different things for 2-3 hours now, and I still can't get it working.
Child Activity:
Intent resultIntent = new Intent(myColorPicker.this, WidgetConfig.class);
resultIntent.putExtra("key", appwidget_notecolor);
setResult(RESULT_OK, resultIntent);
finish();
Parent Activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Toast does not show
Toast.makeText(getApplicationContext(), "onActivityResult fired", Toast.LENGTH_SHORT).show();
if (resultCode == RESULT_OK ) {
// Toast does not show
Toast.makeText(getApplicationContext(), "Result Recieved", Toast.LENGTH_SHORT).show();
}
}
I launch child activity from parent activity like this:
Intent myColorPickerIntent = new Intent(WidgetConfig.this, myColorPicker.class);
myColorPickerIntent.putExtra("appwidget_notecolor", appwidget_notecolor);
WidgetConfig.this.startActivity(myColorPickerIntent);
Of course you won't get the result, you're calling startActivity() instead of startActivityForResult().
You don't seem to be calling startActivityForResult() after creating the Intent.
Are you pass the intent to startActivityForResult() method?

Categories