0

I really want to know how can I transfer data from one activity to another but here is the twist I want it to make a custom cardview and store information in it.

Every time someone enters data it makes a cardview with their information... Is this possible?

Alberto Ursino
  • 366
  • 3
  • 18
itsRayZ
  • 11
  • 2
  • 3
    Does this answer your question? [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Kasım Özdemir May 07 '20 at 14:51

1 Answers1

-1

Using static keyword

static int a=10;                                                                             
static void display()
{
  System.out.println(a+5);
}      

You can call static method directly with your activity class.Remember static method can only hold static members.

Classname.method();
Classname.variable;

Another way to pass data is using Intent & Bundle:- Make intent with your destination class and add key with it's value.Put this code in that activity from where you want to send data.

Intent intent = new Intent(context, DestinationActivity.class);
intent.putExtra(Key, Value);
startActivity(intent);

Now to receive the data

Intent intent = getIntent();
String str = intent.getStringExtra(Key);

To get diff. types of data use getIntExtra(),getFloatExtra(),getStringExtra()

To access other class variables/methods Make Object of your activity class and use it with "." seperator in order to use class variables or methods.

Classname objname = new Classname();
objname.variable;
objname.method();

Ex:-Suppose class name is DemoActivity having variables int a,b and add().

DemoActivity objdemo = new DemoActivity();                                      
objdemo.a;                                                                  
objdemo.add();
  • This is not the correct way to pass data between activity, you have to use the bundle object to pass data between activities – Osama.070032 May 08 '20 at 00:04
  • @Osama.070032 Yes you are right with use of Intent & Bundle you can also pass data between two activities.Thank you for the edit but using the static and object of class class data can also be accessed/pass. – Siddharth Shah May 08 '20 at 06:24