I am trying to so the following on android studio : Sending the user to another app (ie a second app like Facebook or or Twitter ) from my android app that I developed.then , if the user closes the second app, I need to go back to the first app (ie my app) and show a "toast message " on it.
After some search, I have found some way of sending the user from my app to the second app if the user a presses a button on my app.
However, I did not know how to print a "a toast message " if the user closes the second ?
Any help would be really apprecaitd
There is a activity lifecycle callback called onResume()
( onStart()
also works), this callback is called when the user comes back to your app, so you can make a toast message in this method
void onResume(){
super.onResume();
Toast.makeText(...).show();
}
But this method is also called when the app starts, so maybe you need a boolean flag to distinguish those situations
void onResume(){
super.onResume();
if(isBack){
Toast.makeText(...).show();
isBack = false;
}
}
I would suggest to use startActivityForResult
when you start second activity. Then in onActivityResult
, you can do whatever you want (eg: show Toast message
) when second activity finish.
Assume first activity class is called FirstActivity
, & second activity is SecondActivity
.
In FirstActivity
, do this when you want to start SecondActivity
:
// use "startActivityForResult" instaed of "startActivity"
Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, "START_SECOND_ACTIVITY");
To detect finish of second activity
, add this in first activity
:
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if (requestCode == "START_SECOND_ACTIVITY") {
// show Toast message
Toast toast = Toast.makeText(this, "SecondActivity finish", Toast.LENGTH_SHORT);
toast.show();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.