Call back of onActivityResult() in different startup modes of startActivityForResult() in Android

Keywords: Android

As an Android Developer, I'm sure everyone is familiar with startActivityForResult(), so I won't teach you how to do it here. However, it is necessary to share with you the callback of startActivityForResult() in different activity startup modes.

1. The activation mode is standard

<activity android:name=".ui.AActivity" />
<activity android:launchMode="standard" android:name=".ui.BActivity"/>
/**
 2. AActivity The logic is relatively simple, startActivityForResult jumps to BActivity
 3. Then print the onActivityResult() callback information
 4. 
 5. Created by XQM on 2018/4/3.
 */
public class AActivity extends BaseActivity{
    private int REQUEST_CODE = 1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_set);
    }

    @OnClick(R.id.btn_move)
    public void onViewClicked(View view) {
        Intent intent = new Intent(this,BActivity.class);
        startActivityForResult(intent,REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == 2){
            String name = data.getStringExtra("name");
            Log.e("AActivity",name+"Back, AActivity Callback:"+"-------onActivityResult()--------");
        }
    }
}
**
 * BActivity It's also very simple
 * 
 * Created by XQM on 2018/4/3.
 */

public class BActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent();
        intent.putExtra("name","BActivity");
        setResult(2,intent);
        finish();
    }
}


B returns A result and definitely calls back onActivityResult() method

2. The activation mode is single top

<activity android:name=".ui.AActivity" />
<activity android:launchMode="singleTop" android:name=".ui.BActivity" />


B returns A result and also calls back onActivityResult() method

. 3. The activation mode is single task
You can think about the result first and then look at it. Guess that the result A returned by B will call back the onActivityResult() method, under the authentication below.

<activity android:name=".ui.AActivity" />
<activity android:launchMode="singleTask" android:name=".ui.BActivity" />


As expected, the result is consistent with the conjecture. B returns A to call back onActivityResult(), and then looks at the last situation.

. 4. The activation mode is singleInstance

<activity android:name=".ui.AActivity" />
<activity android:launchMode="singleInstance" android:name=".ui.BActivity" />


Very shocked! onActivityResult() can be called back in all four activation modes. So the following question: about the startActivityForResult() method, if A jumps to B, the launchMode property of B is singleInstance, when will the onActivityResult() callback method of A be called?
Can you do it?

Posted by codered27 on Wed, 01 Apr 2020 01:04:05 -0700