i currently have a menu item that looks like the following
case R.id.action_settings_rate_app:
Intent intent3 = new Intent(Intent.ACTION_VIEW);
intent3.setData(Uri.parse("market://details?id=com.raptools.app.raptools"));
startActivity(intent3);
bRet=true;
break;
where it says .setData(Uri.parse i would like to call a string and place the link market://details?id=com.raptools.app.raptools in the string.... is this possible to do?
the reason i want to do this is because i have over 25 java files that all call the menu and have the same links in them..... if i could call the string i would only need to change the one string value instead of having to change all the menu items one by one
thanks in advance
It's totally possible, for that you need handle the links, see this link.
So, in your Activity you can get the parameters.
eg.:
MyActivity1
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("my-scheme://my-domain.com/params1"));
startActivity(intent);
MyActivity2
#Override
protected void onCreate(Bundle savedInstanceState) {
...
Uri data = getIntent().getData();
String p = data.getLastPathSegment(); // Variable p is params1.
}
Look into using a Java Properties file to store this value (and possibly others) for your application. If you do this, you can just change the values in the .properties file and your code can pick up the changes.
Hi as per my understanding with your
You want to place link into string.so that it will be easy for you to change at one place
Let me know if I misunderstand your question
You can add link into String/ String builder
For example
Initialize string variable with link
Like
String urlLink="market://details?id=com.raptools.app.raptools";
case R.id.action_settings_rate_app:
Intent intent3 = new Intent(Intent.ACTION_VIEW);
intent3.setData(Uri.parse(urlLink));
startActivity(intent3);
break;
You can add string variable into separate class and declared String variable as public static into it
So that using class name you will get the urlLink wherever you want in the application
Related
I have 2 activities in my assignment: MainActivity and Country_Activity.
I'm trying to pass 2 inputs the user puts in MainActivity:
int counter
String Country
But the app always crashes here: (this is Country_Activity)
private void Update(){
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intCounter", 0);
String country = mIntent.getStringExtra("country");
counterTextView.setText(intValue);
countryTextView.setText(country);
if (country.equals("canada")){
flagView.setImageResource(R.drawable.canada);
}
else if (country.equals("us")){
flagView.setImageResource(R.drawable.us);
}
}
Specifically on the lines "setText" for each variable.
Everything else works. I can't figure out why they wouldn't.
Thanks!
Usually the extras that are passed from one activity to another are read inside on onCreate() where all the needed initialization of variables and views is made.
In your case I see that you get the extras inside another method (maybe it's called inside onCreate()?).
So you forgot to initialize the textviews:
TextView counterTextView = findViewById(R.id.countersomething);
TextView countryTextView = findViewById(R.id.countrysomething);
also another error that you will encounter later is this:
counterTextView.setText(intValue);
change it to:
counterTextView.setText(String.ValueOf(intValue));
Don't pass an integer value inside setText() because it will be treated as the id of a resource.
I've searched on many SO questions , they are to old and can't find better solution on docs.oracle.com , i don't want to convert Each StringBuilder to string to pass an string array so how to achieve it ?
From inside the source Activity:
Intent intent = new Intent(this, DestActivity.class);
intent.putCharSequenceArrayListExtra("strings", myStringBuilders);
startActivity(intent);
You might have to do something like new ArrayList(Arrays.asList(myStringBuilders));
Make it into a Collection and then pass it through an intent, and then convert it back to an Array.
To pass an StringBuilder array you can use the putExtra of Intent, like this:
Intent it = new Intent(this, YourActivity.class);
StringBuilder[] sbAr = new StringBuilder[4];
sbAr[0] = new StringBuilder("Array0");
sbAr[1] = new StringBuilder("Array1");
it.putExtra("sbParam", sbAr);
however I do not know exactly why when you will retrieve the value in activity targets the StringBuilder array is recovered as CharSequence array.
CharSequence[] csA = (CharSequence[]) getIntent().getExtras().get("sbParam");
I hope this helps
The best way I've seen to pass around objects without bundling is to use an EventBus (I recommend greenrobot/EventBus).
An EventBus can be much faster than bundling. See this article.
The only down side I can see is that if your Activity/process is destroyed because of low memory it will lose the data when it's recreated; Whereas, if you store data in a bundle, the bundle will be resupplied to the Activity when it is recreated (could be wrong here).
Here's how it's done:
Create a POJO to store the StringBuilder for the event:
public class SaveStringBuilderEvent {
public StringBuilder sb;
public SaveStringBuilderEvent(StringBuilder sb){ this.sb = sb; }
}
Create first Activity, and post a StickyEvent before starting the next Activity (a 'StickyEvent' will keep the last event in memory until it is manually removed):
public class Activity1 extends Activity {
private StringBuilder sb;
...
// Function for loading the next activity
private void loadNextActivity(){
EventBus.getDefault().postSticky(new SaveStringBuilderEvent(sb));
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);
}
...
}
Create Second Activity and remove the stick event from the EventBus:
public class Activity2 extends Activity {
StringBuilder sb = EventBus.getDefault()
.removeStickyEvent(SaveStringBuilderEvent.class)
.sb;
}
NOTE:
Personally I think it's the most proper way to handle such a situation where you don't want to bundle an object, however there are two other, less ideal, ways to maintain the state of an object between Activity life-cycles -- you could store the StringBuilder in the Application class or in a Singleton object; however, I wouldn't recommend this because:
If you store it in Application class then you liter variables from all Activities into the Application.
In addition, since the Application/Singleton have global scope, you have to make sure to null the variable so it can get garbage collected or it will stick around for the entire lifetime of the application, just wasting memory. However, using the eventBus, the variable is only stored "globally" between the postSticky and removeStickyEvent commands, so it will get garbage collected whenever those Activities do.
Main activity:
Intent intent = new Intent(Main.this, Secondary.class);
intent.putExtra("name",value);
startActivity(intent);
Secondary activity:
String value = getIntent().getStringExtra("name")
What's wrong here? I've searched a lot without success...
Thanks
Try this:
In the MainActivity:
//Make sure Secondary is an Activity name. Secondary.class.
Intent intent = new Intent(MainActivity.this, Secondary.class);
intent.putExtra("name",value);
startActivity(intent);
In the Secondary Activity:
String value = getIntent().getExtras().getString("name");
You need to get the bundle first and then extract the string from it.
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
bundle.getString("name");
}
Both should work. The second one is to check if the bundle is null.
When you call putExtra(...), make sure the value object is a String. If you're passing any other object, make sure to explicitly call value.toString(), especially if dealing with GUI components.
See here for more information: Android Intent.getStringExtra() returns null
I have used this method times. Just make sure value has a value or initialized.You can use Log or System.out.println(value); after .putExtra to see(in console tab) if value is null. and in second activity too.
Change this line
String value = getIntent().getStringExtra("name");
TO this line
String value = getIntent().getString("name");
I have discovered I have an issue in my code it was my fault. It wasn't an Intent problem. Thank you all.
You can try this -->
intent.putExtra("name", textView.getText().toString());
If the error still occurs, check-in the Second Activity that you are using the right id path...:-
value = findViewById(R.id.);
I want to send string values of latitude and longitude to another class but I'm a little bit confused about how to use it with put extra intent.
Here's my code:
Intent i = new Intent(getApplicationContext(), PlacesMapActivity.class);
//Sending data to another Activity
String latitude = Double.toString(placeDetails.result.geometry.location.lat);
String longitude = Double.toString(placeDetails.result.geometry.location.lng);
i.putExtra("user_lat", latitude);
i.putExtra("user_lng", longitude);
startActivity(i);
Is it right if I try to retrieve the string value like this?
// Getting intent data
Intent i = getIntent();
// Users current geo location
String user_lat = i.getStringExtra("user_latitude");
String user_lng = i.getStringExtra("user_longitude");
Please give me your opinion. Thanks.
You must use the same name for the extra in put and in get. For example you put user_lat but then try to get user_latitude, this obviously won't work. Otherwise, it looks fine to me.
Note that you can put double values directly, there is no need to convert to a String. To get them back use getDoubleExtra.
According to your question - yes, it is correct way to pass data from Activity to another one, but you have to remember that your name argument should be the same for putting and retrieving data.
Also the second way to do this is to use getExtras() method. In the second Activity, in which you want to retrieve data:
// Getting intent data
Intent i = getIntent();
Bundle extras = i.getExtras();
// Users current geo location
if(extras != null) { // Check if extras were found
String user_lat = extras.getString("user_latitude");
String user_lng = extras.getString("user_longitude");
}
The difference between both ways is:
getStringExtra() method returns null if String with specified name could not be found
getExtras() method returns null if no extras was found
I am trying to pass data between two activities in my android application however when
i try the run the on click method that sends the data the app crashes.
This is the code of the activity that works out my calculation is trying to send it to another activity called result. The variable output I am trying to send is a double.
Intent myIntent = new Intent(BMIMetric.this, result.class);
BMIMetric.this.startActivity(myIntent);
myIntent.putExtra("key", output);
Then on the results page I am trying to take the variable with this code
Intent myIntent = getIntent();
double output = (Double) getIntent().getExtras().get("Key");
First, you have an order issue (edited code):
Intent myIntent = new Intent(BMIMetric.this, result.class);
myIntent.putExtra("key", output);
BMIMetric.this.startActivity(myIntent);
You need to set extras before starting the new Activity.
Then in your other Activity do:
Intent myIntent = getIntent();
double output = getDoubleExtra ("key", -1.0);
getDoubleExtra() seems like a better fit since you're assigning to a primitive data type.
Also, as Blumer mentioned, "key" had a different spelling. You need the same spelled key, that's how it works. Otherwise you're mentioning something different and it won't be found.
And as an addition to using getExtras() - if you use getExtras().get() and the key is not found, you will get null in return. Although Doubles can auto-box/unbox nowadays, if you do
Double doubleObject = null;
double d = doubleObject;
You will still get a NullPointerException.