I am launching activities from the main activity in my app using the call startActivityForResult(intent, ACTIVITY_TYPE)
, and they are all working but one.
It also triggers if you have FLAG_ACTIVITY_NEW_TASK
in your intent.
Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, REQUEST_CODE);
Android 4.4 has a small problem about waiting for the return at the end of the actvity closure To solve this behavior it is important to set :
TaskAffinity = "[SAME STRING]"
) launchmode=singleTop,
launchIntent.SetFlags(0); // for reset default Intent flags if you launch from package manager
This solution works with all version of Android
See this for taskAffinity: https://asyoulook.com/computers%20&%20internet/android-onactivityresult-being-called-instantly/1004072
In Android Manifest set android:launchMode="singleTop"
for activity you want open with result and while opening activity set flag intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
If you defined android:noHistory="true"
in the activity in your AndroidManifest.xml, it will cause the same issue here.
Also, check if android:noHistory="true" on activity in Manifest, if yes, it will not work.
For ActivityGroup
or TabHost
and others, maybe the xxxActivity
is a subActivity
of its parent. Then the startActivityForResult
can not work but the parent can get the result.
call getParent().startActivityForResult()
from your sub-activity
your parent (the ActivityGroup
) will be able to handle the onActivityResult
. So I created a subclass of ActivityGroup
and handled this onActivityResult
.
You can re-route that result back to the sub-activity if you need to. Just get the current activity by getLocalActivityManager().getCurrentActivity()
. My sub-activities inherit from a custom activity so I added a handleActivityResult(requestCode, resultCode, data)
in that subclass for the ActivityGroup
to call.
example: http://www.cnblogs.com/relinson/archive/2012/03/25/startActivityForResult.html