Consider the code:
public void callPerson(String phonenumber){
String tel = "tel:" + phonenumber;
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(tel));
startActivity(intent);
// onPause();
// Toast.makeText(getActivity(), "this is a test toast", Toast.LENGTH_LONG).show();
}
As you would expect, when running that code, the current activity pauses itself as the subsequent activity (phone dialer) runs. When the phone call is over, the original activity pops back up.
My problem arises when I desire to introduce a "rating" dialog that pops up after the call.
Where would I insert such code? Well, if we insert it directly after the line, "startActivity(intent);" this would be the same(?) as uncommenting the last line (the toast). If you do this, you will see that the code continues to run after the line "startActivity(intent);" -- the toast appears DURING the phone call.
So how do we prevent the code from continuing to run?
You can see I tried inserting "onPause()" also, but this doesn't seem to work, either.
I understand the Android lifecycle, so the only way I can think to do this is to put the new dialog code in onResume()... but I would also have to do something like:
@Override
public void onResume(){
if (we are currently returning from phone call) {
//Dialog code here
}
}
But it seems like there must be a better way. Thanks for any help!
As you have found out, calling startActivity() does not stop the remaining code in the method from being called. The solution you are proposing would be suitable for your problem. Simply have a class variable such as returningFromCall
:
public void callPerson(String phonenumber){
String tel = "tel:" + phonenumber;
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(tel));
startActivity(intent);
returningFromCall = true;
}
And in the onResume method check if it's true, and reset it:
@Override
public void onResume() {
if (returningFromCall) {
showDialog();
returningFromCall = false;
}
}