I want to pass a specific String from a fragment to an activity to use it there.
When I am using Intent and Bundle, the value is null. I have tried several methods but can't seem to make it correctly. Can someone help, please?
In my Fragment :
Intent testintent = new Intent(getActivity(), MapActivity.class);
Bundle bundle = new Bundle();
bundle.putString("testing", "This is a test");
testintent.putExtras(bundle);`
And in my Activity :
String message = getIntent().getStringExtra("testing");
System.out.println("Value is : " + message);`
And this is what I am getting as a result in my console :
I/System.out: Value is : null
In the Fragment :
val intent = Intent(this, MapsActivity::class.java)
intent.putExtra("testing", "This is a test")
startActivity(intent)
In the receiving Activity :
val extras = intent.extras
if (extras != null) {
val receivingString = extras.getString("testing")
System.out.println("Value is : " + receivingString)
} else {
}
Using jave:
Intent intent = new Intent(getActivity(), MapActivity.class);
intent.putExtra("testing", "This is a test");
getActivity.startActivity(intent);
Related
When I try to pass an int from an activity to another the receiver gets the default value every time.
This is my code from the sender activity
Intent go = new Intent(this,BattleActivity.class);
go.putExtra("Tag", tag);
startActivity(go);
and this is the code from the receiver activity
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("Tag", 0);
tagView = (TextView) findViewById(R.id.tag);
tagView.setText("The selected number is " + intValue);
Probably this from :
Intent go = new Intent(this,BattleActivity.class);
Instead, try:
Intent go = new Intent(SenderActivity.this, BattleActivity.class);
I'm still learning AS & java and recently bought an app code but it has "getArguments()" inside of fragment which I'd like to convert it into activity. Help is appreciated.
This is the code :
String weburl = getArguments().getStringArray(MainActivity.FRAGMENT_DATA)[0];
String data = getArguments().containsKey(LOAD_DATA) ? getArguments().getString(LOAD_DATA) : null;
if (data != null) {
browser.loadDataWithBaseURL(weburl, data, "text/html", "UTF-8", "");
} else {
browser.loadUrl(weburl);
}
How do I write the same code in an activity?
I think what you are looking for is how to send info to an Activity, for that you need Intents
Please verify this info: https://developer.android.com/reference/android/content/Intent
Basically:
public static final String EXTRA_MESSAGE = "extra message";
...
Intent intent = new Intent(this, SecondActivity.class);
String message = "Hello this is an intent";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
To retrieve that data you have to do:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
I invite you to check this out: https://developer.android.com/training/basics/firstapp/starting-activity#java
Intent extras are the activity equivalent of fragment arguments.
I am wondering how I can accomplish checking if an activity was started with an intent.
What I have tried:
I have tried checking if the object was null, but due to my setup, I cannot check that. I have also tried executing with a code, but that failed too.
My code:
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("Title", one);
intent.putExtra("Description", two);
/////////////////////////
Intent intent = getIntent();
String title = intent.getStringExtra("Title");
String description = intent.getStringExtra("Description");
Many thanks!
All activities are started with an Intent,
But we can check is Intent has detail in bundle or not?
like in your case -
Intent intent = getIntent();
if( intent!= null && intent.getExtras() != null
&& !intent.getExtras().getString("Title").equals("")
&& !intent.getExtras().getString("Description").equals("") ) {
// Activity started with sending title & description
}
else {
// Activity started without sending title & description
}
just replace this line.
Intent intent = getIntent();
To
Intent intent = getIntent().getExtra();
you forgot to add .getExtra().
I have a List Activity that when the user presses a line item, it passes the ID of the record to the next activity.
Intent intent = new Intent(CustomerListActivity.this, CustomerEditActivity.class);
intent.putExtra("CUSTOMER_ID", id);
startActivity(intent);
I can see the data in the intent when debugging on that activity; however, when I get into the next activity, the data is not coming up with the below code.
Intent i = getIntent();
Bundle b = i.getExtras();
String s = b.getString("CUSTOMER_ID");
I have debugged and poked around in the variables window, but I do not see the Customer_ID=# value as I do on the previous activity.
You should call String s = b.getStringExtra("CUSTOMER_ID");
Try this.It will work.Paste it on your next activity where you want to get data from intent.
String s= getIntent().getExtras().get("CUSTOMER_ID")+"";
This is how you can do it in a right way
//main activity
Intent intent = new Intent(getActivity(), TargetActivity.class);
intent.putExtra("ParamKey", "key_value");
getActivity().startActivity(intent);
****
//TargetActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.target_activity_layout);
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.get("ParamKey") != null)
paramValue= extras.getString("ParamKey", "default_value");
}
You will have to call getStringExtra() method in New Activity to retrieve the value from Intent.
For example :
String custIdInNewAct= getIntent().getStringExtra("CUSTOMER_ID");
Bundle contains data sent by the calling activity so
Bundle b = getIntent().getExtras();
String s = b.getString("CUSTOMER_ID")`;
I'm trying to pass a string between two activities. I've done this in other projects using the same method, but for some reason I'm getting a NullPointerException when I call intent.getStringExtra(String). I have also tried creating a Bundle for the extras via
Bundle b = getIntent().getExtras();
but that also returned null. Below is the code that I am currently trying to use.
Activity A:
Intent myIntent = null;
String select = "";
if (selection.equals("Chandelle")) {
myIntent = new Intent(Commercial.this, Chandelle.class);
select = "Chandelle";
} else if (selection.equals("Eights on Pylons")) {
myIntent = new Intent(Commercial.this, EightsOnPylons.class);
select = "Eights on Pylons";
}
// Start the activity
if (myIntent != null) {
myIntent.putExtra("selection", select);
Log.d("*** OUTBOUND INTENT: ", "" + myIntent.getExtras().get("selection"));
startActivity(myIntent);
}
Here's the code in activity B that tries to pull the extra:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
if (i == null)
Log.d("***DEBUG****", "Intent was null");
else
Log.d("**** DEBUG ***", "Intent OK");
String MANEUVER_ID = i.getStringExtra("selection"); //Exception points to this line
Log.d("*** DEBUG", rec + " " + MANEUVER_ID);
I've tried pretty much every alternative way of passing extras, but they all seem to behave this way. What am I missing?
Add .ToString() to myIntent.putExtra("selection", select); so that it is myIntent.putExtra("selection", select.ToString());
Luckily I found this post. I've been struggling with this for hours and in the end I realized that I'm sending my intent to the tabHost as well. ;-) For those who are still having problems with this just get the extras from the tabHost activity itself and pass it along to the child tabs.
String userID;
.
.
Bundle getuserID = getIntent().getExtras();
if (getuserID != null) {
userID = getuserID.getString("userID");
}
...
intent = new Intent().setClass(this, Games.class);
intent.putExtra("userID", userID);
spec = tabHost.newTabSpec("games").setIndicator("Games",
res.getDrawable(R.drawable.tab_games))
.setContent(intent);
tabHost.addTab(spec);
Try the code below. I have added an override to your code, which will do the trick:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
if (i == null)
Log.d("***DEBUG****", "Intent was null");
else
Log.d("**** DEBUG ***", "Intent OK");
String MANEUVER_ID = i.getStringExtra("selection"); //Exception points to this line
Log.d("*** DEBUG", rec + " " + MANEUVER_ID);
}
Look at your code, I think it may happen when you did put anything in Activity 1. Because you use "if...else if", and you don't have "else" for other cases. If that happens then you will get null value in the Activity 2. Check again this case.
If you still have not put anything in the bundle, it will always return null!
You can create and set the bundle by your own :
Intent i = new Intent( ... );
i.putExtras(new Bundle());
Bundle bundle= i.getExtras(); // (not null anymore)
Or, put a dummy value just to force the initialization (I prefer the first solution):
Intent i = new Intent( ... );
i.putExtra("dummy", true);
Bundle bundle= i.getExtras(); // (not null anymore)
My problem was resolved when I figured out not to startAcitvity() before I used putExtra().
Newbie mistake but I hope somebody will find it useful.
I changed this
Intent i1 = new Intent(MainActivity.this,Second.class);
startActivity(i1);
String abc = e1.getText().toString();
i1.putExtra("user",abc);
to this
Intent i1 = new Intent(MainActivity.this,Second.class);
String abc = e1.getText().toString();
i1.putExtra("user",abc);
startActivity(i1);
In activity B you at the following function:
protected String getStringExtra(Bundle savedInstanceState, String id) {
String l;
l = (savedInstanceState == null) ? null : (String) savedInstanceState
.getSerializable(id);
if (l == null) {
Bundle extras = getIntent().getExtras();
l = extras != null ? extras.getString(id) : null;
}
return l;
}
You are using it in the following way in activity B:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String MANEUVER_ID = getStringExtra(savedInstanceState, "selection");
}