I know this is a very asked question but i have done my research on it and still didn't manage to come up with a valid solution.
Problem is i try to send data from fragment like this:
Intent intent = new Intent(getActivity(),EditReceiptActivity.class);
System.out.println("RECEIPT IN SETTINGS: "+ receipt);
intent.putExtra("receipt_to_be_edited", receipt);
startActivity(intent);
this is done in a fragment context.
and then when i try to retrive this data in an activity context:
#Override
protected void onResume() {
Bundle extras = getIntent().getExtras();
if (extras != null) {
Receipt value = (Receipt) extras.getSerializable("receipt_to_be_edited");
System.out.println("RECEIPT ON RESUME: "+ value);
FolderDataSource f = new FolderDataSource(getApplicationContext());
f.open();
String name = f.getFolderName(value.getId());
f.close();
folder_title.setText(name);
details_txt.setText(value.getDetails());
value_txt.setText(value.getValue());
category_picked_txt.setText(value.getCategory());
folder_picked_txt.setText(name);
currency_picked_txt.setText(value.getCurrency());
location_picked_txt.setText(value.getLocation());
setImageView();
}
super.onResume();
}
I get a null pointer exception... even though the prints are there... the null pointer exception is on the line:
String name = f.getFolderName(value.getId());
EDIT
The problem was my method getFolderName(), that was returning null, so there is nothing wrong with my passing information code!
So this could turn out to be yet another example of how to pass information from fragment to activity via Intent!
Thanks!
Related
please help .
Im trying to get access to one specific activity from two other activity so I wont write multiple code .
I send from those different activities the same type of "putExtra" but with different values to identify the source of the activity it came from.
I would like if someone could tell me what Im doing wrong.
Sorry and thanks in advance ...
Is this what you're looking for?
Activity1
Intent i = new Intent(Activity1.this, DestinationActivity.class)
i.putExtra("OriginActivity", "Act1")
startActivity(i)
Activity2
Intent i = new Intent(Activity2.this, DestinationActivity.class)
i.putExtra("OriginActivity", "Act2")
startActivity(i)
DestinationActivity
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras != null) {
if(extras.getString("OriginActivity").equals("Act1")){
// you came from Activity 1
}else if(extras.getString("OriginActivity").equals("Act2")){
// you came from Activity 2
}
etc.
You don't need to send extra values to identify the calling Activity, there's a method called getCallingActivity() which can help you.
But it would only return a non null value if you have invoked your Activity through startActivityForResult()
Here is the sample code:
ComponentName callingActivity = getCallingActivity();
if (callingActivity != null)
{
String activityName = callingActivity.getShortClassName();
if (activityName.endsWith("INVOKING_ACTIVITY_NAME"))
{
//do stuff
}
}
I have an activity from where I start a second activity like so:
public void onItemSelected(long id) {
// start the detail activity for the selected item ID.
Intent detailIntent = new Intent(this, FeedDetailActivity.class);
detailIntent.putExtra(FeedDetailFragment.ARG_ITEM_ID, id);
startActivityForResult(detailIntent, DETAIL_REQUEST_ID);
}
In the secondary activity I do this:
if (getArguments().containsKey(ARG_ITEM_ID))
{
long id = getArguments().getLong(ARG_ITEM_ID);
[...]
}
But I get a class cast exception when that getLong is executed stating the parameter is a java.lang.Integer. I was running this in the debugger and noticed that the Intent is created with id of type Long, but it is received with id of type Integer with the value set to 0 (see screenshots).
The Intent as I create it
The intent as I receive it
What is going on?
You are passing Intent to FeedDetailActivity. But reading extras from the FeedDetailFragment which is inside the FeedDetailActivity I think so. So getting wrong value.
Try this approach
First of all read extras from onCreate method of FeedDetailActivity as we read read from an activity.
if(getIntent().hasExtras(ARG_ITEM_ID) {
id = getIntent().getLongExtra(ARG_ITEM_ID);
}
Create Fragment using beginTransaction().replace(/*YourContainer*/, FeedDetailFragment.getInstance(id))
Create a function in FeedDetailFragment as
public static FeedDetailFragment getInstance(long userId) {
FeedDetailFragment mFragment = new FeedDetailFragment();
Bundle mBundle = new Bundle();
mBundle.putLong(ARG_ITEM_ID, userId);
mFragment.setArguments(mBundle);
return mFragment;
}
Now you can read your ID from FeedDetailFragment
if (getArguments() != null && getArguments().containsKey(ARG_ITEM_ID)) {
ID= getArguments().getLong(ARG_ITEM_ID, -1);
}
I want an activity to be started only from another Activity, so I am trying to check if this activity is started from that Activity. If not I want to disable the fields and pop up an alert. However no matter how I try to check for this, I get a NullPointerException
Here is my attempt.
Intent intent = this.getIntent();
if (intent != null && intent.getStringExtra("uid").equals(null)) {
showAlert();
disableFields();
} else {
mUID = getIntent().getStringExtra("uid");
}
How can I refactor this to pass?
Here is my NPE
intent.getStringExtra("uid").equals(null) won't save you from a NullPointerException, since you'll be calling a method on a null reference if there's not extra with key "uid". You can use the hasExtra() method to check whether that extra is passed with the Intent.
IN case ur string is null edit ur if condition as
if(intent != null && intent.getStringExtra("uid")!=null)
I am creating an application, surprising I know, and using a quick back button to return the user to a previous listing. The code below is the intent portion that starts the activity. Now it is sending the activity back to itself with an "LvPos" variable to determine which position it just re-start itself at.
Spinner spinMe = (Spinner)findViewById(R.id.spinner1);
Intent backIntent = new Intent(null, null, getBaseContext(), MainActivity.class);
int itemSelected = spinMe.getSelectedItemPosition();
backIntent.putExtra("LvPos", itemSelected);
startActivity(backIntent);
Now the code below is the reference in the onCreate method that gets teh LvPos variable. The problem is, when I get to this portion, the LvPos is null. I have the same code for various other intents and all work fine. If anyone can see any glaring issues, let me know as I have to be severely overlooking something.
int positionID = 0;
Bundle extras = getIntent().getExtras();
if (extras != null){
String LvPosBundle = extras.getString("LvPos");
if (LvPosBundle != null)
positionID = Integer.parseInt(LvPosBundle);
}
Thank you in advance.
You are Using the putExtra(String, int) when you are putting the extra.
When retrieving you use:
extras.getString("LvPos");
instead use:
extras.getInt("LvPos");
Store that in an integer instead of string. Then you also don't have to do the parseInt.
Hope this Helps.
-Travis
I explained this badly originally. This is my question: The Intent I send to the startActivity() method, contains a private field, mMap, which is a Map containing the strings I sent to putExtra(). When the target activity starts, a call to getIntent() returns an Intent that does not contain those values. The mMap field is null. Obviously, something in the bowels of the View hierarchy or the part of the OS that started the new activity created a new Intent to pass to it, since the object IDs are different.
But why? And why are the putData() values not carried fowrard to the new Intent?
The activity that starts the new activity extends Activity. Here's the startup code:
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case 4:
i = new Intent(this, StatList.class);
i.putExtra("Name1", "Test1");
i.putExtra("Name3", "Test2");
startActivity(i);
}
}
I've tried the key values with and without the (recommended) complete package name prefix.
In the Eclipse debugger, I have verified the values for the player names are being inserted into i.mExtras.mMap properly.
Here's the code from the startee:
public class StatList extends ListActivity {
private final StatsListAdapter statsAdapter;
public StatList() {
statsAdapter = StatsListAdapter.getInstance(this);
} // default ctor
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent i = getIntent();
final Bundle extras = i.getExtras();
< more code here >
}
When execution gets to this method, mIntent.mExtras.mMap is null, and mIntent.mExtras.mParcelledData now contains some values that don't look sensible (it was null when startActivity() was called). getIntent() returns mIntent.
I've also tried startActivityForResult(), with the same result.
From the docs and the samples I've seen online & in the sample apps, this should be easy. I've found another way to meet my immediate need, but I'd like to know if anyone can help me understand why something this simple doesn't work.
In your main Activity:
i = new Intent(this, StatList.class);
i.putExtra("Name1", "Test1");
i.putExtra("Name3", "Test2");
startActivity(i);
Then in StatList.class
Bundle extras = getIntent().getExtras();
String name1 = extras.getString("Name1");
String name3 = extras.getString("Name3");
Log.i("StatList", "Name1 = " + name1 + " && Name3 = " + name3)
Update the following two line
final Intent i = getIntent();
final Bundle extras = i.getExtras();
Replace it with
Bundle extras = getIntent().getExtras();
if(extras!= null){
String var1= extras.getString("Name1");
String var2= extras.getString("Name2");
}