Checking if user came from intent - java

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

Related

Problem sending data to other activity in Android Studio

I want to send data from three activities to the last activity, how can I do
1. First Activity
else {
//first activity want transfer data last activity
Intent intent = new Intent(Register.this, Register2.class);
intent.putExtra("name", reg_name);
intent.putExtra("surname", reg_surname);
intent.putExtra("email", reg_email);
startActivity(intent);
}
2. Second Activity
else {
//second activity transfer data last activity
Intent intent = new Intent(Register2.this, PhoneNumberOtp.class);
intent.putExtra("username", reg_username);
intent.putExtra("password", reg_password);
startActivity(intent);
}
3. Third Activity
else {
//third activity transfer data last activity
Intent intent = new Intent(PhoneNumberOtp.this, OTPVerification.class);
intent.putExtra("phoneNumber", reg_phone_number);
startActivity(intent);
}
Last Activity
else {
Intent intent = new Intent(OTPVerification.this, HomeActivity.class);
//get data register activity
String name = getIntent().getExtras().getString("name");
String surname = getIntent().getExtras().getString("surname");
String email = getIntent().getExtras().getString("email");
String username = getIntent().getExtras().getString("username");
String password = getIntent().getExtras().getString("password");
Extras you put on an intent are not automatically passed along to subsequent activities after the first one they are sent to directly. If you have some data you want to pass through multiple activities, you have to re-add it to the new sending intent for each step. For example:
First
Intent intent = new Intent(UserActivity.this, AccountActivity.class);
intent.putExtra("name", reg_name);
intent.putExtra("email", reg_email);
startActivity(intent);
Second
Intent intent = new Intent(AccountActivity.this, RegisterActivity.class);
// get the received data and add it to the new intent
Intent recv = getIntent();
if( recv != null ) {
String name = recv.getStringExtra("name");
intent.putExtra("name", name);
String email = recv.getStringExtra("email");
intent.putExtra("email", email);
}
// then add any new data
intent.putExtra("username", reg_username);
intent.putExtra("password", reg_password);
startActivity(intent);
Third
// RegisterActivity now has 4 strings available
Intent recv = getIntent();
if( recv != null ) {
String name = recv.getStringExtra("name");
String email = recv.getStringExtra("email");
String username = recv.getStringExtra("username");
String password = recv.getStringExtra("password");
}
In the example you posted, the last activity would only have access to phoneNumber, since that's all you passed it from the third activity.

Transfer data between two activities [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
How to manage startActivityForResult on Android
(14 answers)
Closed 5 years ago.
I am trying to send and receive data between two different activities. I have seen some other questions asked on this site but no question has dealt with preserving the state of the first class.
For example if I want to send an integer X to class B from class A then to do some operations on integer X and then send it back to class A, how does one go about doing this?
Is it as simple as the following code?
in Class A
Intent i = new Intent(this, ActivityB.class);
i.putExtra("Value1", 1);
startActivity(i);
and to receive the response from Class B:
Bundle extras = getIntent().getExtras();
int value1 = extras.getint("Value1",0);
in Class B
Bundle extras = getIntent().getExtras();
int value1 = extras.getint("Value1",0);
//Do some operations on value1 such as maybe adding or subtracting
Intent i = new Intent(this, ActivityA.class);
i.putExtra("Value1", 1);
startActivity(i);
This does not seem correct as I simply want to switch back to Activity A and receive the data from Activity B once the action is completed (perhaps a button in Activity B commences operations on the received data and then sends it back to Activity A?)
In First activity :
Intent i = new Intent(this, ActivityB.class);
i.putExtra("Value1", "1");
startActivityForResult(i, 100);
Receive data like :
Intent receivedIntent = getIntent();
if(receiveIntent!=null){
String value1 = receiveIntent.getStringExtra("Value1");
}
After some operations In second activity:
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
Handle result in FirstActivity :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
}
}
}
startActivityForResult() and onActivityResult() is your solution.
In your ActivityA. Use startActivityForResult() -
Intent i = new Intent(this, ActivityB.class);
i.putExtra("Value1", 1);
startActivityForResult(i, requestCodeForOperation);
And on your ActivityB, get your data sent from ActivityA. Like -
int value1 = getIntent().getExtras().getInt("Value1", 0);
Do your operation and use setResult() for adding you operation result and finish(). like-
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
And of course you need to implement onActivityResult() on ActivityA to get returned data from ActivityB. Like -
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == requestCodeForOperation) {
if(resultCode == Activity.RESULT_OK){
String result=data.getIntExtra("result", 0);
}
}
}

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

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.

Restart MainActivity or application with singleTop.

How can I restart my application or MainActivity when the activity have:
android:launchMode="singleTop"
I have tried:
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
But because of singleTop this not working, there is an other way to do this?
I did my theme switcher like this:
Intent intent = getIntent();
finish();
startActivity(intent);
Basically, I'm calling finish() first, and I'm using the exact same intent this activity was started with. That seems to do the trick?
UPDATE: As pointed out by Ralf below, Activity.recreate() is the way to go in API 11 and beyond. This is preferable if you're in an API11+ environment. You can still check the current version and call the code snippet above if you're in API 10 or below. (Please don't forget to up-vote Ralf's answer!)
public void reload() {
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
or
private void restartFirstActivity()
{
Intent i = getApplicationContext().getPackageManager()
.getLaunchIntentForPackage(getApplicationContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity(i);
}
try to restart with this may this will help u
Intent i = cntxt.getPackageManager().getLaunchIntentForPackage(cntxt.getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addCategory(Intent.CATEGORY_HOME);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
cntxt.startActivity(i);
Use this:
Intent finishIntent = new Intent( this,
activity_that_you_want_to_return.class);
finishIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(finishIntent);

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