简体   繁体   中英

How to pass data from one android activity to an Java class?

MainActivity(Activity)

String emailid=e1.getText().toString();
Intent i = new Intent(MainActivity.this,LongOperation.class);
i.putExtra("emailid",emailid);

LongOperation(Java class )

Intent i = getIntent();
a = i.getStringExtra("emailid");

ERROR MESSAGE:

'getIntent(java.lang.String)' is deprecated as of API 15: Android 4.0.3 (IceCreamSandwich)

In 1st Class :

Intent i = new Intent(MainActivity.this, LongOperation.class);
                    i.putExtra("emailid", emailid);
                    startActivity(i);

In 2nd Class :

Intent i = getIntent();
   String emailid= i.getStringExtra("emailid");

Intents are used to pass data from one activity to another activity.

So if you want to pass data to a normal Java class i would suggest you to use a getter function in combination with a static java class.

So LongOperation has to look like that:

public class LongOperation {
    private static String eMailID;

    // gives the variable eMailID in this class the new value mailID
    public static void setMailID(String mailID) {
        eMailID = mailID;
    }

    // add additional code here
}

In the MainActivity you have to do sth like:

String emailid = e1.getText().toString();
// set the variable in the LongOperation class
LongOperation.setMailID(emailid);

I hope I got your question right, there are several other ways but that depends on the structure and logic of your code. In general I would recommend to work not so much witch static, more object orientated. But in this case static might be easier. Cheers

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