0

I wonder if it is possible to pass string with intent to another Activity without opening that activity.

I am making Android app with 3 layouts and 3 Activities. Firt is MainActivity which is main screen. Then i have SettingsActivity and URLClickActivity. I start app with MainActivity then i must go to SettingsActivity and type in EditText my web adress. Then i go back to MainActivity and then to URLClickActivity. I tried to pass string url_adress directly from Settings to UrlClick.

     Intent i = new Intent(SettingsActivity.this, URLClickActivity.class);
     i.putExtra("int_adress", adress);
     startActivity(i);

I tried with this code but when i run in it closes the Settings and open URLClick Activity.

Is it possible to do it somehow to direcly pass string from SettingsActivity to URLClick Activity or i must first pass string to MainActivity and then to URLClickActivity.?

AKK
  • 23
  • 5

4 Answers4

0

No this is not possible.
But to achieve what you are explaining, you should use the startActivityForResult() method.
Here is the official tutorial to help you implement this method.

Here is another implementation.

Community
  • 1
  • 1
Simon Marquis
  • 7,248
  • 1
  • 28
  • 43
0

As Simon said that its not possible.

  • Instead of using extras in Intent, you can also save the URL in SharedPreference in your SettingsActivity and then retrieve it in your URLClickActivity directly.

  • Otherwise you can use startActivityForResult as Simon suggested. Retrieve the url back to MainActivity and then start the URLClickActivity with the url as extra.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
0

What you are referring to, is storing a persisted value for another Activity to consume and/or read. To do this you could store it into SharedPreferences and then read that preference in the UrlActivity.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
0

Try it using my way -

First create this method in URLClickActivity.class -

public void uRL(String url){

    String vURL = url;
    //vURL is a local variable which will store the url passed from SettingsActivity

}

Now in SettingsActivity, add this code -

URLClickActivity urlActivity = new URLClickActivity();
urlActivity.uRL(adress);

Once you call the method from above code, you will have address stored in vURL.

Confuse
  • 5,646
  • 7
  • 36
  • 58