如果吐司已经出现显示另一个吐司

时间:2013-11-09 01:11:58

标签: android class android-activity android-toast

我想知道当用户打开活动时是否有办法不获取相同的消息,在我的活动打开活动时,如果用户返回我想要的同一活动,您将看到一条带有吐司的消息提出代码的另一条消息:

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.q_050);

  //--> Type mismatch: cannot convert from String to int    

Bundle value = getIntent().getExtras();
            int num = value.getString("sent").toString();
            if(num){
                Toast.makeText(getApplicationContext(), "next message", Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.checkpoint), Toast.LENGTH_SHORT).show();
            }

2 个答案:

答案 0 :(得分:1)

这是你的第一个活动:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.q_050);
    Bundle value = getIntent().getExtras();
    String str= value.getString("sent").toString();
    if(str =="activity_two"){
        Toast.makeText(getApplicationContext(), "next message", Toast.LENGTH_SHORT).show();
    }
    else{
        Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.checkpoint), Toast.LENGTH_SHORT).show();
    }

}   

这是你的第二项活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
    returnButton = (Button) findViewById(R.id.returnButton);//button to return to previous activity
    returnButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(SecondActivity.this,
                    FirstActivity.class);
            intent.putExtra("sent", "activity_two"); //here 1 is a number used in first activity for if else
            startActivity(intent);

        }
    });
}

答案 1 :(得分:0)

在活动A:

上创建一个字符串
String test = "blahblahblah";

创建一个包:

Bundle  b = new Bundle();

然后,将上面的字符串放到包中:

b.putString("testing", test);

定义您的活动B:

Intent i = new Intent(this, YourActivityB.class);

把你的额外包装:

i.putExtras(b);

开始活动:

startActivity(i);