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);
}
}
Related
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
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);
I have Four Activities in my application.
Pressing a button on first three activities Leads To the Fourth Activity. I Used intent For Starting the fourth activity.
In the Fourth Activity. I have A text View Which Changes Its text As Follows: -
if the Activity 4 Was started by Activity 1: The text changes to 0.
if the Activity 4 Was started by Activity 2: The text changes to1.
if the Activity 4 was started by Activity 3: the text Changes to 2.
I Declared an intent like this
For First Activity
Intent intent = new Intent(this, Main4Activity.class);
intent.putExtra("callingActivity",0);
startActivity(intent);
For second Activity
Intent intent = new Intent(this, Main4Activity.class);
intent.putExtra("callingActivity",1);
startActivity(intent);
For Third Activity
Intent intent = new Intent(this, Main4Activity.class);
intent.putExtra("callingActivity",2);
startActivity(intent);
And at Fourth Activity, to fetch the Data I used intents like this.. :
public int checkCallingActivity() {
int callingActivity;
callingActivity = getIntent().getIntExtra("callingActivity", 7);
return callingActivity;
}
But when i run the Programme and Call the Activity 4 from either activity 1,2 or 3, it doesn't take the intent value.. It takes the Default value which is 7 in this case. What is the logical error in the code and how it can be resolved?
Use below code for getting activity name if MainActivity is calling Activity for another Activity
String callingActivityName = MainActivity.class.getSimpleName();
Change this line:
callingActivity = getIntent().getIntExtra("callingActivity", 7);
with:
callingActivity = getIntent().getIntExtra("callingActivity", 0);
Hope it helps.
In your Main4Activity's onCreate() method, try this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
............
...................
int callingActivity = 0;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
callingActivity = bundle.getInt("callingActivity");
}
textView.setText(String.valueOf(callingActivity));
}
Instead of:
callingActivity = getIntent().getIntExtra("callingActivity", 7)
Do this:
int callingActivity = getIntent().getExtras().getInt("callingActivity");
This should work.
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);
I want to pass data from login page to main activity which contains my menus which works fine. But the problem is that suppose a user clicks on a particular menu and open another activity lets say activity A. when the user press the go back button to switch to main activity the bundle gives a null pointer exception. Here is my login intent
Bundle m=new Bundle();
m.putString("userid",userid);
Intent intent=new Intent(login.this,main.class);
intent.putExtras(m);
startActivity(intent);
///////////////
main
String userid;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle b=getIntent().getExtras();
}
Above code works fine but the problem arise when main activity creates another activity A. when Activity A want to go back to parent activity (main) activity gives a null pointer exception.
Intent inte=new Intent(A.this,main.class);
startActivity(inte);
Why not store the userid in SharedPreferences.
Setter:
SharedPreferences pref = getApplicationContext().getSharedPreferences("UserAccount", 0);
Editor editor = pref.edit();
editor.putString("userid", userid);
editor.commit();
Getter:
pref.getString("userid", "default_value");
Clear:
editor.remove("userid");
editor.commit();
You are trying access bundle when returning to Main Activity, but you dont create bundle in your intent. Check for it as follows,
if(getIntent().getExtras() != null)
{
Bundle b=getIntent().getExtras();
}
If you want the bundle in activity A then send the bundle to Activity A and when you are coming back from Activity A then call the intent with bundle to main activity again.
Use this:
Intent i = new Intent(this, main.class);
i.putExtra("Key1", "ABC");
i.putExtra("Key2", "123");
// Starts main.class
startActivity(i);
Now in main.class
Bundle extras = getIntent().getExtras();
if(extras!=null){
String a = extras.getString("Key1");
String b = extras.getString("Key2");
}
Hope this will work..