0

Use the simple login example. Upon successful login, display the email of the user after the Hello message.

My question is how to display the email to Inner Activity

The code is

LoginAcitvity

public class LoginActivity extends AppCompatActivity {

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

        final EditText email = (EditText) findViewById(R.id.emailText);
        final EditText password = (EditText) findViewById(R.id.passwordText);

        final Button loginButton = (Button) findViewById(R.id.loginButton);

        loginButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if (authenticate(email.getText().toString(), password.getText().toString())) {

                    Intent innerIntent = new Intent(LoginActivity.this,
                            InnerActivity.class);

                    startActivity(innerIntent);


                } else {
                    // uname.setText("");
                    password.setText("");
                }
            }
        });

    }


    private boolean authenticate(String email, String password) {
        // Return random value. Later we will contact the server here

        if(email.equals("foo@bar.123") && password.equals("hello")) {
            return true;
        }
        if(email.equals("bar@foo.123") && password.equals("world")) {
            return true;
        }
        return false;
    }
}

InnerAcitvity

public class InnerActivity extends AppCompatActivity {

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

    }
}
kgandroid
  • 5,507
  • 5
  • 39
  • 69

3 Answers3

3

Use Intent. Write this in loginButton.setOnClickListener of LoginActivity:

Intent innerIntent = new Intent(LoginActivity.this,InnerActivity.class);
innerIntent.putExtra("email",email.getText().toString());
startActivity(innerIntent);

and in InnerActivity.java

String email=getIntent().getExtras().getString("email");
txtView.setText(email);

Before proceeding to other topics I will recommend you to learn what a Intent actually is and its vast functionalities. See this and also this.

kgandroid
  • 5,507
  • 5
  • 39
  • 69
1

Send Email throught Intent To Innner Activity like given below.

Intent innerIntent = new Intent(LoginActivity.this,
                        InnerActivity.class);
intent.putExtra("EMAIL",email.getText().toString());
startActivity(innerIntent);

And On Inner Activity get the Intent Data.

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

    String email=getIntent().getExtras().getString("Email");
    mTextview.setText(email);

}
Pradeep Gupta
  • 1,770
  • 1
  • 9
  • 23
0

You can add public static in LoginActivity, and call it from InnerActivity

    public class LoginActivity extends AppCompatActivity {
    public static String loginEmail = "";

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

        final EditText email = (EditText) findViewById(R.id.emailText);
        final EditText password = (EditText) findViewById(R.id.passwordText);

        final Button loginButton = (Button) findViewById(R.id.loginButton);

        loginButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if (authenticate(email.getText().toString(), password.getText()
                        .toString())) {

                    Intent innerIntent = new Intent(LoginActivity.this,
                            InnerActivity.class);

                    startActivity(innerIntent);

                } else {
                    // uname.setText("");
                    password.setText("");
                }
            }
        });

    }

    private boolean authenticate(String email, String password) {
        // Return random value. Later we will contact the server here

        if (email.equals("foo@bar.123") && password.equals("hello")) {
            loginEmail = email;
            return true;
        }
        if (email.equals("bar@foo.123") && password.equals("world")) {
            loginEmail = email;
            return true;
        }
        return false;
    }
}

And call it form InnerActivity

String email = LoginActivity.loginEmail;
kgandroid
  • 5,507
  • 5
  • 39
  • 69
m.qadhavi
  • 84
  • 3
  • You must activate before the Activity to give value to the variable loginEmail... coz it print only the Hello message not the email .. any help on that ? – Dinos Masouras Feb 04 '16 at 10:37
  • its will have the value if you call authenticate function and the email is foo@bar.123 or bar@foo.123. – m.qadhavi Feb 05 '16 at 01:02