0

I have a java class and here is the code

public class credentials {

    public static String getName(String name) {
        String name1 = name;
        return name1;
    }

}

Heres how I pass a value

credentials.getName("Sample Name");

my prob here is how can i get that value

How can I get the value that i passed from another classes?

Its like im passing it using another activity and I need to get it from another

myown email
  • 139
  • 1
  • 10

2 Answers2

0

This is a very basic example use to show how to pass data from one activity to another(here I use Shareprefrence)

     --------------------------Activity1-----------------------------------------  

             editText1=(EditText)findViewById(R.id.ed1);
            editText2=(EditText)findViewById(R.id.ed2);
            but=(Button)findViewById(R.id.btn);
            sp=getSharedPreferences("mypref",MODE_PRIVATE);

            but.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String s=editText1.getText().toString();
                    String s1=editText2.getText().toString();



                    SharedPreferences.Editor editor=sp.edit();

                    editor.putString("user",s);
                    editor.putString("id",s1);
                    editor.commit();
                    gotomainactivity();


                }
            });

            String userId=sp.getString("user",null);
            String password=sp.getString("id",null);
            if(userId!=null && password!=null){
                gotomainactivity();
            }

        }

        private void gotomainactivity() {
            Intent intent=new Intent(MainActivity.this,Sharedatarecive.class);
            startActivity(intent);
            finish();
        }
    -----------------------------------------------------------------------------------Activity2------------ 

// receive data from activity1

     sep=getApplicationContext().getSharedPreferences("mypref",MODE_PRIVATE);
            TextView textView=(TextView)findViewById(R.id.t1);
            TextView textView1=(TextView)findViewById(R.id.t2);

            String s3=sep.getString("user",null);
            String s4=sep.getString("id",null);

    //display data in anctivity2


            textView.setText(s3);
            textView1.setText(s4);
Vishal Sharma
  • 1,051
  • 2
  • 8
  • 15
  • Why would someone use SharedPreferences to pass data between activities? That's not what SP is intended for –  Jul 17 '18 at 10:39
0

If you want to send data from one activity to another then you can send it through intent.

FirstActivity, sending data to SecondActivity through intent.

    Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
    intent.putExtra("Credential","your credential");
    startActivity(intent);

SecondActivity, receiving data from Firstactivity.

    String getCredential = getIntent().getStringExtra("Credential");

Or

Just create a Model class and make them all the variables and methods static, so It can be accessed without an object of Model class.

public class Model{

    public static String name;

    public static String getName() {
        return name;
    }

    public static void setName(String xName) {
          name = xName;
    }
}

FirstActivity

 Model.setName("opriday");

SecondActivity

 String getName = Model.getName();
Shahzad Afridi
  • 2,058
  • 26
  • 24