0

I am having a headache understading how to pass values to an activity inside a library.
I have created a library that contains an activity ( Lets call that activity ShowEmailActivity).
Inside ShowEmailActivity I have got a variable called email of type string and a function called setEmail that takes as a parameter a string.
(example: fun setEmail(email:String){this.email=email})
What i want to do is to show this email as a textview inside my xml ( this is not hard, I know how to do this )
When I add my library as a module depedency from File ->Project structure -> Dependencies ( I then select my main application ) -> + Module depedency Everything works well.
I can even call the ShowEmailActivity from my MainActivity inside my application.
However, what i want to do is to call setEmail function from ShowEmailActivity so that i can set my email from the MainActivity.
What i thought of doing was to create an object of that class inside my MainActivity Class:

val showEmailActivity = ShowEmailActivity() 
showEmailActivity.setEmail(myEmail)

However , when i open an intent to open my ShowEmailActivity , nothing happens.
I am clearly doing something wrong, can anybody suggest anything ?
Thank you.

Nion
  • 149
  • 1
  • 14
  • Look at this answer, https://stackoverflow.com/a/2091482/16552338. You can send your mail by intent. – Mirek Hýbler Sep 03 '21 at 10:39
  • I don't want to send it by intent because I also want to send other data ( like mutable list ) . The email function that I mentioned is just an example. – Nion Sep 03 '21 at 10:50

1 Answers1

1

You are trying to instantiate an Activity and that will not work because this is done by the Android system itself

So if you need to call a specific method say sendEmail in your case as soon as the Activity is created or in Java terms an instance is made and having a parameter as myEmail, you can use the onCreate method of your activity

Let me add some sample code to help you with your case

Say we have your activity ShowEmailActivity , first we will make an intent to start the activity, and we will also pass myEmail as a Bundle argument

Intent intent = new Intent(this, ShowEmailActivity.class);
Bundle bundle = new Bundle();
bundle.putString("email_extra",myEmail); // Here you can add as many bundle arguments as you want
intent.putExtras(bundle);
startActivity(intent);

Now inside your library or where your ShowEmailActivity is defined, we will call the setEmail method inside the onCreate method because onCreate is called as soon as the activity is created by the Android System

class ShowEmailActivity extends AppCompatActivity {

  @Override
  void onCreate(Bundle savedInstanceState){
     if(getIntent.getExtras().getString("email_extra") == null){
        thrown new IllegalStateException("email_extra inside bundle cannot be null"); // Case when someone forgot to pass this bundle extra, this will be thrown
      }
    String myEmail = getIntent().getExtras.getString("email_extra")
    setEmail(myEmail)

  }

  private void setEmail(String email){
     // Your function implementation here
  }

}

Please correct syntatical errors if any, because I'm used to IDE corrections

gtxtreme
  • 1,830
  • 1
  • 13
  • 25
  • Thanks for your response. I already knew how to do that with .setExtra but I was wondering if I could access the setEmail and set the email before I create the intent. As it turns out by what you are saying I can't. Thank you very much for your time explaining it! – Nion Sep 03 '21 at 11:45
  • Why would you exactly want to do it before the intent when the entire logic of that method lies inside the `Activity`? I mean, I just want to understand your user case with it – gtxtreme Sep 03 '21 at 11:48
  • Maybe I am just confused. Let's say for example , we have a recyclerview , when you impement a recyclerview you first call the layoutmanager and then you call the adapter method. I am thinking that all those methods in order to create the recyclerview are prerequirements. If you dont give a layoutmanaer or a adapter then the recyclerview cannot be created. Thats what i have in mind. To create an activity that has prerequirements before you create it. – Nion Sep 03 '21 at 12:20
  • Then you are on the right track my friend, the `onCreate` method is exactly where your `pre-requirements` will go because before that , the `Activity` is just another java object in the memory.Do your pre requirement thing, and then call `setContentView` to set the UI for your user – gtxtreme Sep 03 '21 at 12:22