【问题标题】:android - pass a toast from one activity to another activityandroid - 将 toast 从一个活动传递到另一个活动
【发布时间】:2018-05-31 08:54:35
【问题描述】:

不确定这是否可行,但我正在尝试从 CustomerCall 活动向 RiderHome 活动发送敬酒消息。

一旦司机选择取消。需要向 RiderHome 活动发送祝酒词“司机已取消请求”

这个可以吗?

客户电话

btnCancel = (Button)findViewById(R.id.btnDecline);
    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                cancelBooking();
        }
    });

}

private void cancelBooking() {

    Intent intent = new Intent(getBaseContext(), RiderHome.class);
    String cancel = "cancel"
    intent.putExtra("cancel", cancel);
    startActivity(intent);

    Toast.makeText(CustomerCall.this, "The Driver has cancelled 
    the request", Toast.LENGTH_LONG).show();
}

RiderHome

public class RiderHome extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener, OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {


   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_home);

   }

}

【问题讨论】:

  • 您如何浏览 RiderHome 活动?您能否使用 Intent 包含您的代码 sn-p?
  • 您为什么不干杯 CustomerCall 活动?如果您只是要敬酒,为什么需要将敬酒/消息从另一个活动传递到另一个活动?
  • @TentenPonce 一旦驱动取消请求,需要向 Rider 发送请求被取消的消息
  • 您是指在其他手机/应用程序上吗?或者这只是一个应用程序,因为根据您的标题,从活动到活动。
  • @TentenPonce。我有一个应用程序,您可以在其中以驾驶员或骑手的身份登录。如果用户是 Driver 而另一部手机上的用户是 Rider ...

标签: android android-intent android-toast


【解决方案1】:

你可以这样做-:

private void cancelBooking() {

    Intent intent = new Intent(getBaseContext(), RiderHome.class);
    String cancel = "cancel"
    intent.putExtra("user_cancelled",cancel);
    startActivity(intent);

    Toast.makeText(CustomerCall.this, "The Driver has cancelled 
    the request", Toast.LENGTH_LONG).show();
}

在此传递一个指示驱动程序取消请求的字符串值

在 RiderHome Activity 中,您可以通过以下方式检查您是否获得了该值:

Intent intent=getIntent();
intent.getExtras();

if(intent.hasExtra("user_cancelled"))
{

    You can print your toast here.

}

【讨论】:

  • 感谢您的输入,但没有奏效:/。它是一个具有不同用户的应用程序,所以也许这就是它不起作用的原因。我想如果我只是传递一个字符串,如果传递了字符串,我就可以打印吐司。运气不好,
  • 你能转移到 Riderhome 活动吗?
  • 从 CustomerCall 活动通过一个意图?
  • 是的,意图是通过 CustomerCall 重定向到 Riderhome 活动吗?
  • 是的,它在同一部手机中。我需要它来访问 Rider 应用程序,而不是 Driver 应用程序内。我需要找到一种方法来做到这一点或使它们成为独立的应用程序
【解决方案2】:

而不是(Toast with activity - 活动结束时 Toast 的生命已死)

Toast.makeText(CustomerCall.this, "The Driver has cancelled 
the request", Toast.LENGTH_LONG).show();

to(使用上下文!它会生存)

Toast.makeText(getBaseContext(), "The Driver has cancelled 
the request", Toast.LENGTH_LONG).show();

【讨论】:

    猜你喜欢
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 2017-12-10
    • 2012-07-05
    • 2015-01-14
    • 2014-09-21
    • 1970-01-01
    相关资源
    最近更新 更多