0

I have my variable set on my mainactivity which is a string and i would like to use it in antoher activity / recyclerview

I have tried to use the variable by using this method
import com.<path of my activity where i have my string variable there>;

This showing an error 'Can not resolved symbol'

2
  • 1
    You need to show how exactly the variable is defined in your activity class. It is impossible to import a variable in Java, but Activity is just a normal Java class so may be able to access its variables directly if they are public, or using a getter method if they are private. Commented Sep 11, 2021 at 12:51
  • My apology, i should have write use the the variable not import, thanks for your clarification i just got the answer from @Muliawan, i forgot to add static Commented Sep 11, 2021 at 12:58

2 Answers 2

1

Creating a static variable inside of an android activity, if it holds context (you passed "this" into a constructor somewhere in the static) will cause memory leaks.

I think most people agree the android way to pass variables between activities is with an intent. If the variable is too complicated to pass through an intent, pass enough primitives sufficient to reconstruct it.

And if you know it's going to be used a lot and it's too complex for intents to pass it easily, then it's a good candidate for a singleton (which just means global; you give them scope by declaring them outside of a class and then assign their value wherever you're currently assigning it.)

Sign up to request clarification or add additional context in comments.

Comments

0

First of all, you need to understand the static variable,

A static variable is one that's associated with a class, not objects of that class. Find out more here : https://www.caveofprogramming.com/java/java-for-beginners-static-variables-what-are-they.html

To resolve your issue is quite simple:

First Step:
Make sure in your mainActivity file to have a static variable:
public static String <string_name>;

Second Step:
Make sure in your secondActivity file to put static between import and the path of the mainActivity file.
import static com.<path of your mainActivity>;

And you can access your variable easily in your entire secondActivity

I'm newbie here, please correct me if I'm giving wrong answer

Thanks 😁

3 Comments

Your answer is not technically wrong, but it is a bad practice in both general Java programming and specifically Android. Exchanging data between activities using a static variable may cause various bugs, but exactly what and how would depend on the actual use of the variable. I will not downvote your answer since it does solve the problem, but I recommend avoiding actually using this in real code!
I'm just a newbie on java development, that is awesome if that could your development @Dreamerwoodd
Yepp technically that is a bad practice it is better to use another method such as example passing the variable data with intent between activity. thankyou to mentioned it here as well @LevM.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.