What is the difference between getIntent() and new Intent() in Android? - java

I do like this
first way
Intent intent = new Intent();
intent.putExtra("isLoggedIn",true);
setResult(RESULT_OK,intent);
second way
Intent intent = getIntent();
intent.putExtra("isLoggedIn",true);
setResult(RESULT_OK,intent);
Both can give the same result.I want to know the actual difference between this two

In the context of an Activity, getIntent() will return the Intent that was originally sent to the Activity. The example you gave may work the same, but you should really avoid using getIntent() if you are passing the Intent to another Activity or sending it back as a result.
For example:
If I start an activity with:
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("key", "test");
startActivity(intent);
Then in my MainActivity class:
Intent intent = getIntent();
String value = intent.getString("key"); // value will = "test".
So now consider if you have SecondActivity and I am starting it from MainActivity using getInent();
Intent intent = getIntent();
intent.setClassName("com.example.pkg", "com.example.pkg.SecondActivity"");
intent.setComponent(new ComponentName("com.example.pkg", "com.example.pkg.SecondActivity"));
intent.putExtra("isLoggedIn",true);
startActivity(intent);
Then in my SecondActivity I can access key and isLoggedIn both.
Intent intent = getIntent();
String value = intent.getString("key"); // value will = "test".
boolean testIsLoggedIn = intent.getBooleanExtra("isLoggedIn",true);
So, generally it is not good practice to use the getIntent to start further activities.

Related

Checking if user came from intent

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().

Android Studio: Passing variable through another variable doesn't work

I created a Method for my MainActivity to pass a String to my SecondActivity.
public void convertCurrency(View view)
{
intent.putExtra("NO", "My String");
startActivity(intent);
}
But in my SecondActivity in my OnCreate Method
Button back = (Button) findViewById(R.id.back);
TextView t = (TextView) findViewById(R.id.first_text);
Intent intent = new Intent(this, MainActivity.class);
String g = intent.getStringExtra("NO");
t.setText(g);
Nothing happens. But Why?
get Your variable in this way:
String yourVriable = getIntent().getStringExtra("NO")
don't new Your intent
Your are creating new intent.
In onCreate method call
String data = getIntent().getStringExtra(key);
replace Intent intent = new Intent(this, MainActivity.class);
by Intent intent = getIntent();
When you do intent.getStringExtra("NO"); your intent Object is new, so his Extra is empty.
You need to change the code in both the activities . You need to create intent and put your string into it, so your code will be.
public void convertCurrency(View view) {
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("NO","My String");
startActivity(intent);
}
Note that in the intent constructor the first argument is the current activity and the second argument will be the activity that you are starting.So in the second activity you need to recieve the string. Code for that will be,
Button back = (Button) findViewById(R.id.back);
TextView t = (TextView) findViewById(R.id.first_text);
String g = getIntent().getStringExtra("NO")
t.setText(g);
I resolve the problem myself. There occurs an error if you have two Intent objects with the same name even if they are within separate methods.

How do I get the reference of an activity I opened using intent?

Inside my MainActivity I open my SecondActivity
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
How do I get the reference of this instance of SecondActivity inside my MainActivity?
//inside MainActivity
SecondActivity object = // make it equal to instance created above
Pass your a handler to the second one via trigger intent, like this:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("Handler", handler);
startActivity(intent);
Then in your SecondActivity post message and data, maybe the instance of SecondActivity.this or another you like, to the handler.

Passing an ArrayList of LatLng through intent

Right now, I know how to send an ArrayList of Strings through intent by doing this to send:
Intent intent = new Intent(MainActivity.this, MapActivity.class);
intent.putStringArrayListExtra("list", list);
startActivity(intent);
and this to receive:
Intent i = getIntent();
ArrayList<String> list = i.getStringArrayListExtra("list");
But I want to send an ArrayList of LatLngs, like this:
Intent intent = new Intent(MainActivity.this, MapActivity.class);
intent.putLatLngArrayListExtra("listLatLng", listLatLng);
startActivity(intent);
\\pseudo code, obviously
and then:
Intent i = getIntent();
ArrayList<String> list = i.getLatLngArrayListExtra("listLatLng");
\\pseudo code, obviously
Any ideas on how to do this? Is there any easy way or do I have to modify something? Thanks.
I would use android.location; It extends Parcelable and you can easily create and store the Locations in an arrayList created from lat and lon values
Intent i = new Intent(MainActivity.this, MapActivity.class);
i.putParcelableArrayListExtra(name, value);

Passing data between 2 activities

I am programming in android for a Samsung tablet, and I have 2 activities, one of this is a list of football teams, and the other activity is their twitter, but i have a problem to pass the parameter for the first activity to the second one. I want to pass their url like a string, but i cant get it.
Thanks!
You have to use Intent:
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("extraURL", "http://myUrl.com");
startActivity(i);
Then, to retrieve it in SecondActivity, in the onCreate method do:
Intent receivedIntent = getIntent();
String myUrl = receivedIntent.getStringExtra("extraURL");
You typically launch the 2nd Activity from the first using an Intent. You can pass data to the 2nd Activity using the same Intent you use to launch it. For example,
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("url", "http://url.you.want.to.pass/");
startActivity(i);
In the 2nd Activity, in the onCreate, you can read the data using:
Intent i = getIntent();
String url = i.getStringExtra("url");
your First Activity on click of button
Intent intent = new Intent(this,ActivityTwo.class );
intent.setAction(intent.ACTION_SEND);
intent.putExtra("www.google.com",true);
startActivity(intent);
Receive it in ActivityTwo:-
Intent intent = getIntent();
String msg = intent.getStringExtra("www.google.com");

Categories