I have 2 activities: Activity_A and Activity_B. On Activity_A I have a TextView with the id "MyTextView". How can I get its text from Avtivity_B?
I have tried to do this in Activity_B main Java file:
txtv = (TextView) findViewById(R.id.MyTextView);
txtv.getText().toString()
But this didn't work.
Is there any way to this without Intents?
You should get the content on the Activity where you have it and then send data from activity A to activity B with the intent:
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("fromActivityA",textView.getText());
And then you have to use getExtra() method on activity B for getting the data you send from activity A.
Hope you can find this helpful!
The challenge is that when an activity is not visible, it has been paused, and is no longer available. Also the id used in findViewById may refer to an id in the other activities layout file. Using an intent to pass the data when moving between activities is a good solution, or if you really need access to that data from different activities you could store it in shared preferences, in a database, or as a static field on the activity class itself.
Related
I understand that my question is probably not difficult, but I could not find an answer to it. In a nutshell: I transfer text between activities through an intent. In the second activity, instead of text, I get the following. I have used intent more than once to transfer data between activities and there have never been such problems.enter image description here
from sender activity
String textToSend = someTextView.getText().toString();
Intent intent = Intent(this, ReceiverActivity.java);
intent.putExtra("text", textToSend);
startActivity(intent);
you're currently sending textview as string instead of text from the textview
Make sure you are passing data as string and also when you retrieve in second activity, use "getStringExtra()" method and define a string variable and use
val stringdata = intent.getStringExtra("your_intent_name")
I am making my own project but I am stuck.
Getting the information from the user in the first activity is working just fine.
But when I try to get those numbers, ints, in the third activity via a second activity, it is showing me the default value.
I am trying it using an Intent, but it is not working.
Intent intent = new Intent(this, active.class);
Intent intent2 = new Intent(this, BMR_Active.class);
intent.putExtra(EXTRA_TEXT_height,height );
intent.putExtra(EXTRA_TEXT_weight,weight);
intent.putExtra(EXTRA_TEXT_age,age);
startActivity(intent);
It is giving me a default value i.e. 0(I have given) in BMR activity then i am going via active activity.
Your putExtra method via intent would work fine, but are you also running putExtra BEFORE starting the third activiy?
The data is first passed from first activity to second activity, then from second activity is passed to third activity using the same method.
Additional Note:
In cases like this, I would usually use fragments instead, I would store the data in the activity and fragments will obtain the datas via getActivity(). This will eliminate the needs of multiple passing of data around.
Based on what you said in your comment about passing data straight from Activity 1 to Activity 3, then what you can do is the following:
Page1.class:
Intent toPage3 = new Intent(Page1.this, Page3.class);
int myNum = 5;
toPage3.putExtra("Number", myNum);
startActivity(toPage3);
Page3.class:
Intent receiveIntent = getIntent();
int numFromPage1 = receiveIntent.getIntExtra("Number");
I believe that this should answer your question. numFromPage1 will be an int containing the value from Page1.
So guys I have two quiz and when the user do both of them i want to show into the menu the sum of user's score.Here is the part of my code that has relation with my question:
Quiz 1:
Intent intentt=new Intent(multiplechoicek1.this,menuaskisewn1.class);
intentt.putExtra("scoree",mscore);
startActivity(intentt);
Quiz 2:
Intent intent=new Intent(diagwnismakefalaio1.this,menuaskisewn1.class);
intent.putExtra("score",mScore);
startActivity(intent);
Menu
Intent intent=getIntent();
int score=intent.getIntExtra("score",0);
Intent intentt=getIntent();
int scoree=intentt.getIntExtra("scoree",0);
int athroisma=score+scoree;
currentscore.setText("To σκορ σου είναι: "+ athroisma +"/24");
When i test the app it shows only the sum of correct answers of 1 quiz..i suppose that this is because when it calls the oncreate method when one quiz is over the intent from the second quiz gets the default value...so it nevers sums up the correct answers of both quiz.(I didnt use sharedpreferences cause i dont want to store data after the app is closed).Any ideas? Thank you and sorry for bad English :)
As you are starting "menuaskisewn1" activity from "multiplechoicek1" and "diagwnismakefalaio1" with "startActivity" method, it will create new activity of "menuaskisewn1" and then "menuaskisewn1" will get only extras from that intent which was called last time. so you have only one intent extra instead of both at a time.
Also you don't want to save data in shared preference.
In this case you can create a class which extends "Application" class and then take two static variable within it. In this way you can access those variable in every activity and also you can sum up those two variables in which ever activity you want. By using this way, you don't have to pass those in intent extras.
Make sure your manifest file will use that application class.
It's not clear how and when you execute the code for opening the menuaskisewn1 activity, but it seems that you are opening it twice, why?
Then when menuaskisewn1 is opened, it seems like you want to get 2 different intents with:
Intent intent=getIntent();
but what you are doing is getting the same intent twice and this is the intent that started the activity, because an activity is started by 1 Intent only.
So this intent contains either the extra with key "score" and this is saved in score or the extra with key "scoree" and this is saved in scoree but not both.
So one of the variables score or scoree is 0 and when you add them the result is the same as one of the variables.
What you can do is start only once the activity and put 2 extra values:
Intent intent=new Intent(multiplechoicek1.this,menuaskisewn1.class);
intent.putExtra("score1", mscore1);
intent.putExtra("score2", mscore2);
startActivity(intent);
and get the values when the activity opens:
Intent intent=getIntent();
int score1=intent.getIntExtra("score1",0);
int score2=intent.getIntExtra("score2",0);
int athroisma=score1+score2;
currentscore.setText("To σκορ σου είναι: "+ athroisma +"/24");
I want to create an intent in MainActivity that will launch CompassActivity.
Both classes share a common layout activity_main, but use different parts of it.
Here is the intent I currently have in MainActivity which is supposed to open the CompassActivity.java class file but doesn't.
public void startCompass(View v)
{
Intent intent = new Intent(this, CompassActivity.class);
startActivity(intent);
}
What I have tried:
Altering androidmainfest.xml.
Changing the intent itself.
Changing the SDK version.
All you have to do is place the correct variables under the onCreate method and it should work fine.
Do you want to open an intent or do you just want to show/hide the different views you are using?
Instantiate your Views (findViewById) in your onCreate, then you can call view.setVisibility(View.GONE or View.VISIBILE) to hide or display your different views.
Bottom line, your views exist already on the main Activity (so no Intent needed to start another Activity) - you just need to control which views are visible at a particular time.
I have a litte appwidget with a configuration activity.
In the configuration activity I have a EditText
to store a particular string and a button to create the widget.
In the widget itself there is only a Imageview with a static image showing a button.
when i press the button im doing the following:
AppWidgetManager app_widget_manager = AppWidgetManager.getInstance(context);
RemoteViews local = new RemoteViews(context.getPackageName(), R.layout.main);
app_widget_manager.updateAppWidget(widget_id, local);
Intent create = new Intent(this, MyWidget.class);
create.setAction("com.test.mywidget.CREATE");
create.putExtra("toastmessage", edittext1.getText().toString);
context.sendBroadcast(create);
Intent result_value = new Intent();
result_value.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id);
setResult(RESULT_OK, result_value);
finish();
the extra 'toastmessage' is just the content of the edittext.
in the widget i catch the broadcast in onReceive() and get the string value that was
passed before as extra 'toastmessage' to the new widget.
if i press on my widget, i create a toast with the passed string
- and here is my problem -
the passed string is different for every widget created.
but the variable (in the widget) where i store the passed string
gets resettet after a little time or when the phone gets restartet or the app killed or something similar,
so i need persistent data for every widget instance.
I need to store which widget got which string passed,
and get this data everytime when the widget gets updated.
How can i achieve this?
According with this link you have some options to accomplish your goal.
For shared preferences, here an example.
For internal storage Take this way.
For external storage, you can use this.
For SQL Lite storage, here.
Hope these examples helps, cheers.
The widget_id you received is a unique identifier of the widget instance. Use it as a key to identify your persisted data.