简体   繁体   English

从第二个应用程序返回后,在第一个应用程序上打印“ toast”消息

[英]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.

相关问题 抓住toast事件(来自任何应用程序)并获取toast消息 - Catch toast event (from any app) and get toast message 无法在Android应用上检测吐司消息 - Not able to detect toast message on android app Toast 消息不起作用 android 应用程序 eclipse - Toast Message Not Working android app eclipse 检查是否未安装Android应用,然后提示祝酒消息 - Check if Android app is not installed then prompt a toast message 应用程序崩溃并且不显示Toast消息 - app crash and doesn't display Toast message savedInstanceState 是 null 在第一次应用程序关闭后但不是在第二次 - savedInstanceState is null after first app shutdown but not after second 第二个应用的Android启动活动没有第一个应用的图标 - Android start activity of second app having no icon from first app 如何更改在应用内结算中成功购买产品后的Toast消息 - How to change toast message that comes after successful purchase of a product in in-app billing 从第二个活动返回后在第一个活动中更新 Listview - Update Listview in first activity after returning from second activity Toast未在Android App中显示,但首先显示了Toast,然后在两天后停止显示 - Toast Is not being Displayed in The Android App , But first it was being diplayed then after two days it stopped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM