Android Studio java incorrect display TextView - java

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")

Related

how can forward data form i activity to 3 activity via 2 activity

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.

getting the text from a TextView from a different activiy

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.

Android - how to pass a List of Bitmap to another Activity as parameter

I have a List of Bitmap in an Activity, that i got through async method. Now i need to pass my List of Bitmap to another activity.
Here is the code i tried, but gives me an error...
List<Bitmap> result = bitmapResult
Intent intent = new Intent(PhotoList.this,ImageSlider.class);
intent.putExtra("bitmapList", result);
startActivity(intent);
It gives me an error.
Cannot resolve method 'putExtra(java.lang.String, java.util.List<android.graphics.Bitmap>)'
What is the best practice to do it?
And how can i retrive the List from the new Activity?
Thanks in advance.
Since we are not aware how big is your Bitmap array and could potentially hit the allotted limited of passing over intent. The ideal approach is to store your bitmap as image in your SDCard/Phone storage and gets its Uri. Therefore you only need to send the array of Uri Strings e.g. List<String> rather than List<Bitmaps>
In activity which you need, you have to receive List you send before.
List<Bitmap> result = bitmapResult
Intent intent = new Intent(PhotoList.this,ImageSlider.class);
intent.putExtra("bitmapList", result);
startActivity(intent);
Receive activity;
List<Bitmap> receive = (List<Bitmap> getIntent.getExtra.get("bitmapList");
After that, you using "for loop" to set any item which you need.
You can use ArrayList, it simpler.

android widgets and persistent data

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.

Intent putExtra(String,String) inside of AsyncTask

I'm simply trying to carry a string onto the next activity without having to define an entire object for the task. I've seen similar solutions and gotten them to work BUT without using AsyncTask to create the intent.
protected void onPostExecute(Boolean result) {
if (loggedIn && hasPin) {
Intent intent = new Intent(UniteActivity.this,
WebViewActivity.class);
intent.putExtra(PASSED_USERNAME, passUser);
startActivity(intent);
}
if (loggedIn && !hasPin) {
Intent intent = new Intent(UniteActivity.this,
CreatePinActivity.class);
intent.putExtra(PASSED_USERNAME, passUser);
startActivity(intent);
PASSED_USERNAME is a public static constant to hold the package name, just as the putExtra() method requires. I then try to pull the value out in the next activity.
Intent extras = getIntent();
String username = extras.getStringExtra(UniteActivity.PASSED_USERNAME);
// carry username to next activity
Intent intent = new Intent(CreatePinActivity.this,WebViewActivity.class);
intent.putExtra(PASSED_USERNAME, username);
startActivity(intent);
There is never a String to pull out, the value of username is always null. I've gone through the debugger and found that the Eclipse IDE debugger shows different intent ID's between the activities, they are never consistant. Is it possible that AsyncTask is interfereing somehow because it splits into a seperate thread?
I don't know if this applies to your problem, because I can't see from your code snippet if the intermediary activity is freshly created or not.
BUT: the getIntent()-method always returns the first Intent that started the activity. If the activity remains in the background and receives a new Intent this value does not get updated automatically. You have to override onNewIntent(...) and manually call setIntent(...) for this to work (or do all your stuff directly there).
So just for the case that you do not run your posted code in the onCreate() method please check if you did not miss to fetch the real intent you are interested in.
Not sure of the exact answer for you solution.
You calling startActivity on the UI since it's in postExecute().
If all else fails you can just save that value to a sharedpreference.
The way you have handled the variable PASSED_USERNAME seems incorrect. You have used it in some palaces as simple PASSED_USERNAME whereas in some other places you have used it with the class named prefixed UniteActivity.PASSED_USERNAME. Since it is a Public Static Constant always use it prefixed with the class name.

Categories

Resources