简体   繁体   中英

Android Login and pasting first and secondnames to user area

Hi guys I need your help, I am stuck in that place. I would like to have first and secondname pasted to user area to appear automatically after logging in. The problem is the user area is in different java class than the one which comes next after log in activity.

Here is the part of the code from log in activity

    if(session.loggedin()){
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        finish();
    }



    btnlogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String email = etemail.getText().toString();
            final String password = etpassword.getText().toString();

            Response.Listener<String> responseListener = new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject jsonResponse = new JSONObject(response);
                        boolean success = jsonResponse.getBoolean("success");

                        if(success){

                            String firstusername = jsonResponse.getString("firstusername");
                            String secondusername = jsonResponse.getString("secondusername");

                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.putExtra("firstusername", firstusername);
                            intent.putExtra("secondusername", secondusername);

                            startActivity(intent);
                            finish();

                            session.SetLoggedIn(true);

And here is the code from the user activity

/*
    Code for showing users name and last name
     */

    final TextView firstUsername = (TextView) findViewById(R.id.firstusername);
    final TextView secondUsername = (TextView) findViewById(R.id.secondusername);

    Intent intent = getIntent();
    String firstusername = intent.getStringExtra("firstusername");
    String secondusername = intent.getStringExtra("secondusername");

    firstUsername.setText(firstusername);
    secondUsername.setText(secondusername);

Now I uploaded some more code.

You approach seems correct. First you should check if your extras are not null( for some reason). Bundle extras = getIntent().getExtras(); if (extras!= null)

Or if the variables you pass(the one you deserialize from json) to intent are not empty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM