From my Android app I wish to launch the default web browser to send the user to a verification web page, and then once verified use a callback to send them back to my app.
I have that functionality working ok, but what I’m having difficulty with is intercepting the situation where the user decides they don’t want to verify themselves in the web browser and clicks the back button to return to my app.
I’ve tried launching the web browser with startActivityForResult() so I can intercept back clicks using onActivityResult(), but for some reason onActivityResult() is called immediately after launching the web browser, and not at all when the user clicks back.
Here’s some basic test code that demonstrates the problem:
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MainActivity","onCreate called");
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivityForResult(intent,101);
Log.i("MainActivity","startActivityForResult called");
}
@Override
public void onResume()
{
super.onResume();
Log.i("MainActivity","onResume called");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Log.i("MainActivity","onActivityResult called");
}
}
Output after running this code:
I/MainActivity(545): onCreate called
I/MainActivity(545): startActivityForResult called
I/MainActivity(545): onResume called
I/MainActivity(545): onActivityResult called
I/MainActivity(545): onResume called
Then if the user clicks back from the web browser, it just calls onResume() a third time.
So given that, would anyone have any suggestion on how I can recognise when the user clicked back from the browser?
How about giving this a try: