How to pass a text from one activity to all activities? - android

I need to parse a single text from my MainActivity.java to all other activities in my app.
Is it possible to do so?

Just store the text as string in shared preferences and then get the string in other activities.. or you can also use broadcast receiver in all other activities. But first all the activities should call the receiver first then the MainActivity can send the text.
In MainActivity,
this.getSharedPreferences("MyPrefName", Context.MODE_PRIVATE).edit().putString("parsetext","yourtext").apply();
and in the other activities..
this.getSharedPreferences("MyPrefName", Context.MODE_PRIVATE).getString("parsetext","");

Better way is to pass data between activities. Code:
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.putExtra("parsetext", "your text here");
startActivity(intent);
Access that intent on next activity:
String s = getIntent().getStringExtra("parsetext");

Your question sounds you need global access of some data in your all Activites, whenever something is needed to be accessed temporarily that is, for the time until application is active you can use your Application class, which is globally accessible through getApplicationContext() in all Activites.
For reference see this answer:
https://stackoverflow.com/a/1945297/4878972
But if the data you need to access in all the Activites needs to get saved permanently then you can go for Shared Preference approach.

You can have a public static final String in your MainActivity and access that String from another activity. As in:
In MainActivity.java
public static final String MY_STRING = "my string";
In other places where you need to access the variable, you can access is as:
String string = MainActivity.MY_STRING;

Related

how to pass user input from activityOne to activityThree, activityFour, activityFive in Android?

I'm learning Android and trying to pass user input from my first activity to several activities but not the second activity. actually I'm going to get more data in my second activity and later use that also in my later activities but not in order. How can I do that?
you make the variable as static or you cab send data as extras.
Set data in intent:
Intent intent = new Intent(FirstActivity.this,SecondActiviy.class);
intent.putExtra("key",value);
startActivity(intent);
Fetch data from intent:
String data = getIntent().getStringExtra("key");

Is it Possible to Pass Edittext Value from one activity to another activity without using Intent

Intent intent = new Intent(this, Page.class);
intent.putExtra("arg", getText);
startActivity(intent);
//second activity:
String passedArg = getIntent().getExtras().getString("arg");
enteredValue.setText(passedArg);
To get value from one activity to another activity without using
intents....?? I want to show edittext passed data in other activity
edittext but without using intent...?? Kindly help me is it possible
or not....??
There are many ways: event buses, SharedPreferences, through global object (application class), database, simple file. But why?
Try direct and readable style first.
if you can't bundle the values you want to save in the Intent, then you could use the SharedPreferences. More information here

How can we get the values entered in edittext from one activity to another?

I am devoloping a dictionary application and i have declared two string dynamically..
final String words[]={
"Gamble", "Stress", "Stammer"
};
final String meanings[]={
"To play", "Tension", "To speak with involuntary pauses or repetitions "
};
When the user enters the word in edittext view I want to switch to another activity if the word is found from the above array and show the meaning on the next activity.
How can I fetch the meanings array to display on second activity?
When you want to pass data across Activities, just you use Intent and add data to intent.
Intent i = new Intent(YourActivity.this, Another.class);
i.putExtra("someKey", data);
startActivity(i);
Don't remember to add Activity to your manifest.xml.
<activity
android:name=".YourActivity"
android:label="#string/app_register"
/>
EDIT:
So to retrieve data in another Activity you use this.getIntent().getExtras().getStringArray(<key>) in your case when you want to pass String[].

Transferring int variable between activities Android

I am making a quiz game. However, I need to add a score variable that can be modified. The way the game works is it uses a few different activities. How can I transmit this score and modify it throughout the activities.
Here's an example of passing an integer from one activity to another activity.
Start ActivityA and pass it the int like this:
Intent theIntent = new Intent(this, ActivityA.class);
theIntent.putExtra("somename", intVariable);
startActivity(theIntent);
Get the integer from within ActivityA like this:
int i = getIntent().getIntExtra("somename");
trying this do not work in my activity onCreate-method.
Method do need a default value as second parameter.
int id = getIntent().getIntExtra(DetailClick.ID_NAME,-1);
Hope this helps!

android switchstatement question

I was wondering, how would I make a switch statement, that when that certain case was triggered, it would open a new screen with text. Would I use an intent? And if so, which one?
Thank you for your help in advance.
When you want to open a "new screen", you probably want to open a new activity. You would create a second Activity-derived class and use the following overload of the Intent constructor with startActivity:
Intent intent = new Intent(this, MySecondActivity.class);
startActivity(intent);
this will explicitly attempt to open a new Activity with the class name MySecondActivity
to pass a String of text from one Activity to another in this way, you can add it to the intent.
String someValue = "Some Value";
intent.putExtra("Some Key", someValue);
and in the code of your other Activity, you can get at this string via the Intent
getIntent().getStringExtra("Some Key");
Obviously you want to do null checks to make sure the key exists in the Intent, and you want to put a proper constant String somewhere instead of using a literal String for a key, but this is the basic gist.
Kriem... As Rich said you can launch a new activity using intents and push data to the new activity using extras. You can push data back to your main activity using startActivityForResult instead of startActivity. You can return to your main screen by calling finish() in the new screen. Finally, you can put the new screen event handlers into the NewScreen.java file.
The overall effect is a near full separation from dependency between the two activities, so that you might be able to easily re-use the NewScreen.java class in another project. I have some code here.

Categories

Resources