How to pass String from Fragment to Activity - java

I'm passing an url as string from Fragment to Activity. But I'm getting an error of could resolve getArguments() and it is highlighted in red color
Inside Fragment
Intent i = new Intent(getActivity(), web.class);
Bundle args1 = new Bundle();
args1.putString("url1", "file:///android_asset/em/japan.html");
startActivity(i);
((Activity) getActivity()).overridePendingTransition(0,0);
and inside activity where I want to receive string, I've used
String url1 = getArguments().getString("url1");`
But getArguments() is highlighted in red color.
Thanks in advance.

Intent i = new Intent(getActivity(), web.class);
i.putExtra("url1", "file:///android_asset/em/japan.html");
startActivity(i);
and in your activity use
String url1 = getIntent().getStringExtra("url1");

You need a callback for that, see this: https://developer.android.com/training/basics/fragments/communicating.html
comment below if you need do a sample project for your case :D

Related

how to get from a fragment to an activity without disturbing fragment's reausability

This is now bugging me a lot.
I want to get to an activity from a Fragment without disturbing the reusability of the fragment. That mean I cant directly start an Intent from within the fragment to a the activity the fragment is currently attached to.
Here is the code under question
//This is the code inside the fragment
public void onStart() {
super.onStart();
View view = getView();
switch (position){
//case 1 is the case when the user clicks the ADD List Item from a ListFragment.
//Position is the position of ADD in the List Fragment.
case 1:{
Intent intent = new Intent(/*what exactly should I put here?*/ ,
/*this is where the reference to activity the fragment is attached to goes.
But we dont know what activty the fragment is attached to, as it is reusable
and may get attached to different activity at different times*/);
}
case 2:{
//this is the case when user decides to view the entered text in the array list.
TextView textView = (TextView)view.findViewById(R.id.display_name);
int size = workout.arrayList.size();
Object[] array = workout.arrayList.toArray();
for(int i=0;i<array.length;i++){
textView.setText((String)array[i] + "\n");
}
}
}
}
I feels like the data may be insufficient, although I am not sure what else to provide, sorry about that.
If more data is required, tell me.
Inside a Fragment we get the Context for an Intent using:
Intent intent = new Intent(getActivity(),AnyActivity.class);
OR
Intent intent = new Intent(getView.getContext(),AnyActivity.class);
The AnyActivity() can be any activity including the one currently having the Fragment.
You can open the same activity like this..
Intent intent = new Intent(getActivity(),SameActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
UPDATE To reuse the fragment for different values pass arguments to the fragment...during transaction..
Bundle bundle = new Bundle();
bundle.put("className", "SameActivity");
fragment.setArguments(bundle);
in fragment get the class name from bundle ... different class will pass different arguments as bundle..
In OtherActivity bundle would look like this..
Bundle bundle = new Bundle();
bundle.put("className", "OtherActivity");
fragment.setArguments(bundle);

Passing textview value across activities

I am trying to pass a value to a text view into a different activity but only when the button in the activity is clicked.
Here is the activity I am trying to set the text...
public static Button yes;
public static final String TEST_KEY = "test";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question19);
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
}
});
Here is the HighRiskActivity that is the destination for updating the textview's value...
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
t = (TextView) findViewById(R.id.abusedOrNah);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
String value = extras.getString(TEST_KEY);
t.setText(value);
}
My problem is that no text is printing at the desired activity, am I passing in the wrong data?? Any help would be amazing, this is the last step to the app that I am creating :D
UPDATE
I do not want the Question12Activity to be directed to the HighRiskActivity when the button is clicked. I want it to go to the next activity but still be able to pass the text onto the HighRiskActivity once the button is clicked. Sorry for the confusion, hopefully that makes more sense :)
Try to pass data this way:
Intent i = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“test”, "Data you want to pass");
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);
Now in your SecondActivity class you retrieve data as below:
Bundle bundle = getIntent().getExtras();
//get the data…
String stuff = bundle.getString(“test”);
I don't know what exactly you are trying to achieve by starting the first activity with the statements below
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
But if the Activity you want to start is the HighRiskActivity then the following should fix your issue:
Get rid of the statements related to the Question13Activity mentioned above.
and add the call to start the actual HighRiskActivity like below:
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
startActivity(i); //This line is important to start the new Activity.
So, I guess your major issue here is you didn't start the activity with the startActivity(i); call.
In Activity A you are using the internal intent bundle that is not public to you and private to the intent. In the Activity B you are asking the intent to instead look for a bundle that you provided yourself.
Try something like this in your Activity A
Intent intent = new Intent(context, YourActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("EXTRA1", "string data");
intent.putExtra(TEST_KEY, bundle);
startIntent(intent);
Here is a good writeup on the subject (see answer) Advantages of using Bundle instead of direct Intent putExtra() in Android
You dont need to define two intents in order to connect two Activities. Put the following inside onClick method
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
startActivity(i);

How to pass image URL using intent and bundles in Android Studio?

I am new to Android apps. I'm doing JSON parsing for my listview. When I click on my list Image I need to pass that Image to second activity. I'm trying to pass it using intent and bundles. But I have image URL so I don't know how to pass it. I searched everywhere others are passing bitmaps or id.
Class- FirstActivity
Bundle bundle= new Bundle();
bundle.putString("imageUrl",<url for image>);
Intent i= new Intent(FirstActivity.this,SecondActivity.this);
i.putExtras(bundle);
statrActivity(i)
Class- SecondActivity
Bundle bundle = getIntent().getExtras();
String image_url =bundle.getString("imageUrl");
your first activity
Intent intent = new Intent(MainActivity.this,AnotherActivity.class);
intent.putExtras("IMAGE_URL",your_image_url);
startActivity(intent);
in Second activity where you want data
Bundle bundle = getIntent().getExtras();
String image_url =bundle.getString("IMAGE_URL");
pass image_url from one activity to another activity, and then use picasso lib to show image.
In FirstActivity, in the onClickListener, add this
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtras(SecondActivity.KEY_IMAGE_URL, image_url);
startActivity(intent);
And in SecondActivity
public static final String KEY_IMAGE_URL = "image_url";
#Override
protected void onCreate(Bundle savedInstanceState) {
...
if (getIntent().hasExtra(KEY_IMAGE_URL)) {
String imageUrl = getIntent().getStringExtra(KEY_IMAGE_URL);
}
}

setText to another activity?

I have a button on an activity called "HomePage". When you click the button, I want it to setText to a TextView called "weaponTitle" on a separate activity, called ItemSelection. Though, when I run the setText, it gives the error:
Attempt to invoke virtual method void android.widget.TextView.setText(java.lang.CharSequence) on a null object reference
So this means it can't find the TextView "weaponTitle". Is there a good way to do fix this? I have made sure I set up everything correctly too!
Here is the sliver of code I forgot to share!
new displayPrices(weaponTitle, "Genuine Freedom Staff");
try this
Firstclass
Intent intent = new Intent(getApplicationContext,SecondActivity.class);
intent.putExtra("KEY",value);
intent.putExtra("KEY",VALUE);
startActivity(intent)
Second Activity
Intent intent =getIntent();
confirm_txt =(TextView)findViewById(R.id.txt);
String txt_put =intent.getStringExtra("KEY");
confirm_tId.setText(txt_put);
Inside oncrete of your HomePage Activity
Button yourbutton = (Button) findViewById(R.id.your_button_id);
yourbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent intent = new Intent(HomePage.this, ItemSelection.class);
intent.putExtra("weapontitle",value);
startActivity(intent);
}
});
Inside ItemSelection Activity's oncreate
Intent intent =getIntent();
String txt =intent.getStringExtra("weapontitle");
TextView weaponTitle = (TextView) findViewById(R.id.textview_weaponTitle);
weaponTitle.setText(txt);
go through this way,
pass this from your first activity,
Intent i = new Intent(Login.this, ChangePasswordActivity.class);
i.putExtra("savedUser", savedUser);
Log.e("username", "--->>" + savedUser);
startActivity(i);
in second activity , get this as below
Intent extras = getIntent();
savedUser = extras.getStringExtra("savedUser");
Log.e("savedUser", "--->>" + savedUser);
Now you can set text as you got it on other activity.
etUsername.setText(userName);
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("weaponTitle","Genuine Freedom Staff");
startActivity(intent);
In the second activity you do:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
String weaponName = bundle.getString("weaponTitle");
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(weaponName);

Android get different content in single activity

actually I'm searching a way to show different content in one activity everytime when it's created.Here is what actually I'm thinking to do but not really sure if there is a way and how can I do it. Basically I have two activities. The first one contains a listview with 100 elements on it.I want to be able to show different content in activity 2 when I click a listview item in Activity 1. I need to be able to change two textviews and one imageview.
Any suggestions how can I do that? Thanks in advance!
You want to use Intents to pass Payload between your Activitys.
On Activty1 you make a new Intent like:
Intent myIntent = new Intent(view.getContext(),
Activty2.class);
myIntent.putExtra("detailtext", ((TextView) view).getText());
startActivityForResult(myIntent, 0);
the putExtra Method is for your Payload.
then in Activty2 you can extract the Intent with:
getIntent().getStringExtra("detailtext"));
hope that helps
Do somethins like this :
Activity 1:
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("someKey","someValue");
startActivityForResult(Intent, 0);
Activity 2:
String i = getIntent().getStringExtra("someKey"));
TextView txt = (TextView) findViewById(R.id.textView); //your textview's id
txt.setText(i);
That should work!

Categories