What is use of start activity for result in Androi

2020-02-11 03:32发布

What is use of start activity for result in Android? Please give example and what is difference between startactivity and startactivityforresult?

标签: android
2条回答
我想做一个坏孩纸
2楼-- · 2020-02-11 03:56

startActivityForResult() allows you to start activity and get some data back. Imagine that you have some file picker activity. You can start it and when user chooses the file, the result is given back to the original activity.

Also, it can be used if you simply want to ensure that the second activity has successfully done somethings.

The result code is obtained in onActivityResult method:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Result OK.d.
        if (requestCode == resultCode) {
            // do something good
        }
    }
查看更多
甜甜的少女心
3楼-- · 2020-02-11 04:01

By calling startActivityForResult with Activity2, your current activity will be notified when the Activity2 is finished (back button pressed), and this way you can also get information from it.
This notification you can catch by overriding your activity's onActivityResult method.

This article about Android startActivity and startActivityForResult might be worth to look at.

查看更多
登录 后发表回答