0

I am trying to do that when user is successfully login then it go to firstActivity.class and show message("Welcome"). But if User is failed to login then Toast message Show in Another secondActivity?

BlankFragment:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View v = inflater.inflate(R.layout.fragment_blank, container, false);
            button = (Button)v.findViewById(R.id.btn);
            textview1 = (TextView)v.findViewById(R.id.tv1);
            textView2 = (TextView)v.findViewById(R.id.tv2);
            editText1 = (EditText)v.findViewById(R.id.ed1);
            editText2 = (EditText)v.findViewById(R.id.ed2);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(editText1.getText().toString().equalsIgnoreCase(editText2.getText().toString())){
                        Intent intent = new Intent(getContext(),FirstActivity.class);
                        startActivity(intent);
                    }else{
Note->(How to show this toast message in secondActivity??)
                        Toast toast = Toast.makeText(getContext() , "UserName or Password invalid" , Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }
            });
            return v;
        }
    }

SecondActivity:

?????
McCygnus
  • 4,187
  • 6
  • 34
  • 30
Haider
  • 99
  • 1
  • 13
  • *"How to show this toast message in secondActivity??"*...put that code in the second activity? But I'm not sure why you would want to go to a second activity just to tel them they did something wrong. – codeMagic Apr 12 '17 at 17:49
  • Possible duplicate of [How to manage \`startActivityForResult\` on Android?](http://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android) – fweigl Apr 12 '17 at 17:56

3 Answers3

2

Just show the toast on the current activity.

But if you really want to show the toast on second activity, you can just throw the intent on the second activity. You can do this by following code:

else{
  Intent intent = new Intent(this,SecondActivity.class);
  startActivity(intent);
}

Then on the SecondActivity just create a toast which displays the message :

Toast.makeText(this, "UserName or Password invalid",Toast.LENGTH_SHORT).show();
pm2br
  • 49
  • 7
0

If you start second just to display an error toast message, don't do it. You can display toast message in the current activity. If second activity contains some other logic then you can use next code:

In the current activity:

Intent intent = new Intent(getContext(), SecondActivity.class)
                      .putExtra("show_toast", true);
startActivity(intent);

In the second activity's onCreate() method:

if (getIntent().hasExtra("show_toast") && getIntent().getBooleanExtra("show_toast", false)) {
    Toast.makeText(getContext(), "UserName or Password invalid", Toast.LENGTH_SHORT).show();
}
Sergio
  • 27,326
  • 8
  • 128
  • 149
0

first open second activity using following code

else
{
  Intent in = new Intent(this,SecondActivity.class);
  startActivity(in);
}

following code write in your seconde activity

Toast.makeText(getApplicationContext, "UserName or Password invalid",Toast.LENGTH_SHORT).show();
Firoz Memon
  • 4,282
  • 3
  • 22
  • 32