Android - startActivityForResult immediately triggering onActivityResult

前端 未结 13 1120
感情败类
感情败类 2020-11-27 10:54

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.

相关标签:
13条回答
  • 2020-11-27 11:32

    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);
    
    0 讨论(0)
  • 2020-11-27 11:36

    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 :

    • all activities will have the same task Affinity attribute. (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

    0 讨论(0)
  • 2020-11-27 11:41

    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);

    0 讨论(0)
  • 2020-11-27 11:41

    If you defined android:noHistory="true" in the activity in your AndroidManifest.xml, it will cause the same issue here.

    0 讨论(0)
  • 2020-11-27 11:42

    Also, check if android:noHistory="true" on activity in Manifest, if yes, it will not work.

    0 讨论(0)
  • 2020-11-27 11:43

    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.

    1. call getParent().startActivityForResult() from your sub-activity

    2. your parent (the ActivityGroup) will be able to handle the onActivityResult. So I created a subclass of ActivityGroup and handled this onActivityResult.

    3. 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

    0 讨论(0)
提交回复
热议问题