Question

I wanted to know if there was a way not to get the same message when the user opens the activity, in my activity when it is open you will see a message with the toast if the user returns to the same activity i want to bring up another message the code is :

 @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();
            }
Was it helpful?

Solution

This is your first activity:

    @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();
    }

}   

This is your second activity:

@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);

        }
    });
}

OTHER TIPS

create a string on activity A :

String test = "blahblahblah";

create a bundle :

Bundle  b = new Bundle();

then, put above string to the bundle :

b.putString("testing", test);

define your activity B :

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

put your extras bundle :

i.putExtras(b);

start the activity :

startActivity(i);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top