[英]printing a “toast” message on the first app after returning from the second app
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. 我正在尝试在android studio上执行以下操作:将用户从我开发的android应用程序发送到另一个应用程序(即第二个应用程序,例如Facebook或Twitter),然后,如果用户关闭第二个应用程序,则需要转到返回第一个应用程序(即我的应用程序)并在其上显示“吐司消息”。
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. 经过一些搜索,如果用户a按下我的应用程序上的按钮,我发现了一种将用户从我的应用程序发送到第二个应用程序的方法。
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 有一个活动生命周期回调,称为
onResume()
( onStart()
也有效),当用户返回您的应用程序时会调用此回调,因此您可以使用此方法发送祝酒消息
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. 我建议您在开始第二个活动时使用
startActivityForResult
。 Then in onActivityResult
, you can do whatever you want (eg: show Toast message
) when second activity finish. 然后在
onActivityResult
,您可以在第二个活动完成时做任何您想做的事情(例如:show Toast message
)。
Assume first activity class is called FirstActivity
, & second activity is SecondActivity
. 假设第一个活动类称为
FirstActivity
,第二个活动类称为SecondActivity
。
In FirstActivity
, do this when you want to start SecondActivity
: 在
FirstActivity
,当您要启动SecondActivity
时,请执行以下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
: 要检测
second activity
完成,请将其添加到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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.